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

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -