Home > Enterprise >  JavaScrtipt conditional operators if else if else
JavaScrtipt conditional operators if else if else

Time:09-05

Ask the user for a five-digit number and determine if it is a palindrome. My attempt, but output is always "This is not palindrome"

let a =  prompt("Enter five digit number: ")
if((a > 9999) && (a < 100000)){
    b=a / 10000;
    a=a % 10000 == 0;
    c=a / 1000;
    a=a % 1000 == 0;
    a=a % 100 == 0;
    e=a / 10;
    a=a % 10 == 0;
    if ((b == a)&&(c == e)){
        alert("This is palindromeм");
    }
    else{
        alert("This is not palindrome");
    }
}
else{
    alert("You entered not a five digit number");
}

CodePudding user response:

You just need to check if the input is equal to the reverse of it.

With checking if a === a.split('').reverse().join('')

let a = prompt("Enter five digit number: ")
if (!(a.length === 5)) {
  alert("You didn't enter five digit number");
} else if (a === a.split('').reverse().join('')) {
  alert("You entered a palindromeм")
} else {
  alert("You didn't enter a palindromeм")
}

  • Related