<- function(num){
make_negative ifelse(num <= 0, num, -1*num)
}
5 Return Negative
Tantangan #5/366 - 18 Feb 2024
https://www.codewars.com/kata/55685cd7ad70877c23000102/train/r
8kyu Return Negative
5.1 Instruction
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
Examples
make_negative(1); # return -1
make_negative(-5); # return -5
make_negative(0); # return 0
Notes
The number can be negative already, in which case no change is required. Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.
5.2 YouTube Video
5.3 Solution Code
Solusi bar-bar
Solusi simple
<- function(num)-abs(num) make_negative
5.4 Test
library(testthat)
test_that('Basic tests', {
expect_equal(make_negative(42), -42)
expect_equal(make_negative(-9), -9)
expect_equal(make_negative(0), 0)
expect_equal(make_negative(1), -1)
expect_equal(make_negative(-1), -1)
})
Test passed 😀