10  Opposite number

8kyu Tantangan #10/366 - 24 Feb 2024

https://www.codewars.com/kata/56dec885c54a926dcd001095

10.1 Instruction

Very simple, given a number (integer / decimal / both depending on the language), find its opposite (additive inverse).

Examples:

1: -1
14: -14
-34: 34

10.2 YouTube Video

10.3 Solution Code

opposite <- function(number){
  -1*number
}
opposite <- function(number)-number

10.4 Test

library(testthat)

test_that("the opposite of positive 1 is negative 1", {
  expect_equal(opposite(1), -1)
})
Test passed 😸
test_that("the opposite of 0 is 0", {
  expect_equal(opposite(0), 0)
})
Test passed 🎊
test_that("the opposite of positive 3.14 is negative 3.14", {
  expect_equal(opposite(3.14), -3.14)
})
Test passed 🎉
test_that("the opposite of negative 400.2 is positive 400.2", {
  expect_equal(opposite(-400.2), 400.2)
})
Test passed 🥇

10.5 Supported by

StarCore Analytics