javascript - Trying to find if a number is a palindrome in JS -
i'm trying learn js , have few exercises i'm trying solve in order improve. @ moment, i'm trying find out if number palindrome, , have code, doesnt seem work, since numbers insert in inputs palindromes.
<input type="text" class="screen"> <button type="button" class="btn">check</button> var strg = document.queryselector(".screen").value; var pal = strg.split("").reverse("").join(""); document.queryselector(".btn").addeventlistener("click", function(){ if (strg == pal) { console.log(strg+" palindrome"); } else { console.log(strg+" not palindrome"); } })
https://jsfiddle.net/lw6uk8kb/
appreciate help.
you have move first 2 lines inside handler well:
document.queryselector(".btn").addeventlistener("click", function(){ var strg = document.queryselector(".screen").value; var pal = strg.split("").reverse("").join(""); if (strg == pal) { console.log(strg+" palindrome"); } else { console.log(strg+" not palindrome"); } });
else query same (empty?) value of .screen
.
Comments
Post a Comment