make_negative(1); # return -1
make_negative(-5); # return -5
make_negative(0); # return 0
5 Return Negative
8Kyu Tantangan #5/366 - 05 Feb 2025
https://www.codewars.com/kata/55685cd7ad70877c23000102
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
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
make_negative <- function(num){
ifelse(num <= 0, num, -1*num)
}
Solusi simple
make_negative <- function(num)-abs(num)
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 🎉
Supported by