I am trying to write a code that shows the last digit (in a set of 3) is the same. I could not understand why I got false for each set so, I decided to test -20%10 and I got -0 meanwhile 20%10 gives me the expected 0. What is this -0 about?
function last_digit( x, y, z){
if(x % 10 == y % 10 == z % 10 ){
return true;
}else {
return false;
}
}
console.log(last_digit(20, 30, 400));
console.log(last_digit(-20, 30, 400));
console.log(last_digit(20, -30, 400));
console.log(last_digit(20, 30, -400));
x % 10 == (y % 10 == z % 10). I.e. you comparex % 10with a boolean result. What you want is something likex % 10 == y % 10 && y % 10 == z % 10.===for (pretty much) all comparisons. why? becausetrue == 1checklast_digit(5, 15, 21)with your code.