I need to define a sign
function which returns either -1
or 1
. The Math.sign
function does not suffices since it returns -0
and0
respectively for the inputs -0
and 0
.
My first attempt at solving this was...
function my_sign(a) {
let sign = Math.sign(a);
if (sign === 0 || sign === 1){
return 1;
} else {
return -1;
}
}
Although, since 0 === -0
is true
, this does not work. How to proceed?