簡體   English   中英

JavaScript 條件運算符 if else if else

[英]JavaScrtipt conditional operators if else if else

向用戶詢問一個五位數字並確定它是否是回文。 我的嘗試,但 output 始終是“這不是回文”

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");
}

您只需要檢查input是否等於它的reverse

檢查是否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м") }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM