-2

Write a JavaScript program to check from three given numbers (non negative integers) that two or all of them have the same rightmost digit.

this is sample code:

enter code here

function same_last_digit(p, q, r) {
return (p % 10 === q % 10) ||
       (p % 10 === r % 10) ||
       (q % 10 === r % 10);

}
console.log(same_last_digit(22,32,42));
console.log(same_last_digit(102,302,2));
console.log(same_last_digit(20,22,45));
YetAnotherBot
  • 1,584
  • 2
  • 16
  • 26
anhtran226
  • 11
  • 3
  • 2
    Do you know what the `%` operator does in JavaScript? Have you looked that up? Have you written tests to observe its behavior? What output does this code give you? What output did you expect? Why? – David Nov 04 '17 at 10:11
  • 1
    This smells awfully like a homework question – hardillb Nov 04 '17 at 10:13
  • Check [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder) – Durga Nov 04 '17 at 10:14

2 Answers2

1

% is modulus/remainder operator which returns the remainder when one operand is divided by a second operand

Here in p % 10 p is the first operand which on division by 10 , will produce 2 as remainder

console.log(22%10) // output 2,

console.log(32%10) // output 2,

console.log(42%10) // output 2,

brk
  • 43,022
  • 4
  • 37
  • 61
0

https://en.wikipedia.org/wiki/Euclidean_division

You are dividing by 10 again and again until the remaining value is smaller than 10. This means the remaining value is your last digit. Now you are only comparing the 3 last digits.

Basti
  • 442
  • 3
  • 17