Q:

Write a JavaScript program to check whether all the digits in a given number are the same or not

0

Write a JavaScript program to check whether all the digits in a given number are the same or not.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function test_same_digit(num) {
  var first = num % 10;
  while (num) {
    if (num % 10 !== first) return false;
num = Math.floor(num / 10);
  }
  return true
}

console.log(test_same_digit(1234));
console.log(test_same_digit(1111));
console.log(test_same_digit(22222222));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now