JavaScript(JS) JS check if the numbers have same last digit

h‮:sptt‬//www.theitroad.com

To check if two numbers have the same last digit, we can use the modulo operator % to get the last digit of each number and then compare them. Here's an example:

function hasSameLastDigit(num1, num2) {
  return num1 % 10 === num2 % 10;
}

In this example, the % 10 operation returns the last digit of the number by giving us the remainder when the number is divided by 10. We compare the last digits of both numbers using the === operator and return true if they are the same, and false otherwise.

Here's an example of using the function:

console.log(hasSameLastDigit(123, 23)); // true
console.log(hasSameLastDigit(567, 23)); // false
console.log(hasSameLastDigit(23, 123)); // true

Note that this function only checks the last digit of each number, and not the rest of the digits. If you need to check whether two numbers have the same digits in general, regardless of their position, you would need to convert them to strings and compare each character.