I want to know if it is possible to make a number the opposite to what it currently is using JavaScript. ie if a number is 400
. Is it possible to make it -400
, similar if a number is -400
is it possible to make it 400
?
To use negative numbers, just place a minus (-) character before the number we want to turn into a negative value: let temperature = -42; What we've seen in this section makes up the bulk of how we will actually use numbers.
And the "not" ( ! ) operator returns the opposite of the boolean value to its right. That is, true becomes false and false becomes true .
This is not jQuery!
Just multiply it by -1
.
num = "400"
console.log(-num);
Core JS,
function opposite(number) {
return(-number);
}
As the shorter solution from http://www.codewars.com/
In one Line..
const opposite = number => -number;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With