-3

Hello I am writing code in javascript to find out if 4 digits number is palindrome or not. First I check if the number has for digits and if it does I find every digit using / and % and then check if the first digit matches the last and the second digit matches the third digit. The thing is the result is always number not palindrom.Can someone help me out?

var numri = window.prompt("Vendosni numrin");
numri = parseInt(numri);
while (numri > 9999 || numri < 1000) {
  alert("Number not 4 digits");
  var numri = window.prompt("Vendosni numrin");
  numri = parseInt(numri);
}
var shifra4 = numri % 10;
numri = numri / 10;
var shifra3 = numri % 10;
numri = numri / 10;
var shifra2 = numri % 10;
var shifra1 = numri / 10;

if (shifra4 == shifra1 && shifra2 == shifra3)
  alert("Number palindrome");
else
  alert("Number not palindrome");
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Malv
  • 29
  • 2
  • 7
  • 1
    `number.toString() === number.toString().split("").reverse().join("")` – Nandu Kalidindi Feb 16 '18 at 14:59
  • Read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/4642212). Use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) and try [debugging your program](https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) yourself. – Sebastian Simon Feb 16 '18 at 15:00
  • If you would just look at the numbers in your program you would see why it is always false. Use the debugger or print them. – takendarkk Feb 16 '18 at 15:03

2 Answers2

2

Why not use the string itself? Use split and reverse

var numri = window.prompt("Vendosni numrin");
var items = numri.split( "" );
var isPalindrome = items.join( "" ) === items.reverse().join( "" );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    Although clear, this technique would be slow compared to a “pure numeric” solution. – James Feb 16 '18 at 15:00
  • @James How much slower? Let me check on jsPerf. – gurvinder372 Feb 16 '18 at 15:01
  • @James Wouldn't there be an additional cost of doing a parseInt first? – gurvinder372 Feb 16 '18 at 15:04
  • Why encourage not learning how to do basic numeric operations in favor of a string conversion kludge? – Ken White Feb 16 '18 at 15:04
  • @KenWhite Why not keep the code less verbose using existing array methods? – gurvinder372 Feb 16 '18 at 15:05
  • Because it's a kludge to avoid learning to do it properly. Whatever happened to the days when programmers actually learned to write code rather than convert everything (improperly) to strings? – Ken White Feb 16 '18 at 15:07
  • 1
    @KenWhite Hopefully those days are gone for good. I would rather keep the code less verbose and **more readable** than writing 20 lines to do the same thing for mere few milliseconds performance gain. – gurvinder372 Feb 16 '18 at 15:09
  • 2
    String conversions require memory allocations that are unnecessary, and even with GC that's ridiculous to avoid basic math. In addition, in non-GC languages those repeated allocation/deallocation operations can result in memory fragmentation. There's no excuse for not learning to do things correctly just to save a few lines of code, and (for programmers who bothered to actually learn to code) the numeric ops are just as readable. Script kiddies have trouble with them for sure. Not sure why anyone would say *I hope the days of real programming knowledge are gone for good*. – Ken White Feb 16 '18 at 15:22
  • 1
    @KenWhite: note that in this case, the data *starts* as a String. Besides, the reverse-and-match technique is actually more insightful than a pairwise comparison. And suitably enhanced, it applies to cases of palindromes not expressible with numbers. ("level", "Madam, I'm Adam.") – Scott Sauyet Feb 16 '18 at 16:33
  • @ScottSauyet: The applicability to *palindromes not expressible with numbers* is irrelevant (and clear). The discussion was specifically about numeric to string conversions (and based on the question title, which says *number palindromes*). Clearly the string *reverse-and-match* would be the proper approach when dealing with *strings*. – Ken White Feb 16 '18 at 17:00
  • @KenWhite: I'm suggesting that a more generic solution is often preferable. With a suitable `equals` function, this could also work to describe the palindromic nature of `['a', 'b', 'b', 'a']`. I'm just suggesting that the best way to understand a palindrome is one that includes `reverse` in some guise. And given the number of attempts we see here to do it another way, that insight is not always obvious. – Scott Sauyet Feb 16 '18 at 17:33
  • 1
    @ScottSauyet: And I'm not arguing that at all. I *am* arguing that the strategy of *Let's convert everything to strings for every conceivable use* is not a valid *generic solution*. What's next? *I need to compare two matrices for equality. Let's convert them to strings first to do it*? There are already enough difficulties in questions being asked here at SO about problems that are caused solely because the poster has no concept of doing anything with any data type that is not a string. Not everything contains sequences of characters, and not learning to program any other way is not coding. – Ken White Feb 16 '18 at 17:39
  • 1
    @KenWhite: And I certainly wouldn't argue with that in general. But being a palindrome is not really a numeric property. (It doesn't survive changing bases, for instance.) So numeric methods don't seem particularly appropriate. I'm not arguing that this is merely a tolerable approach. I think in this case it's actually preferable. Using numeric methods to solve what is not a numeric problem is just as problematic as using strings where they don't belong. – Scott Sauyet Feb 16 '18 at 18:45
1

numri/10 can (well in most of the cases) return a float number so you need to take Math.floor() of that value.

var numri = window.prompt("Vendosni numrin");
numri = parseInt(numri);
while (numri > 9999 || numri < 1000) {
  alert("Number not 4 digits");
  var numri = window.prompt("Vendosni numrin");
  numri = parseInt(numri);
}
var shifra4 = numri % 10;
numri = Math.floor(numri / 10);
var shifra3 = numri % 10;
numri = Math.floor(numri / 10);
var shifra2 = numri % 10;
var shifra1 = Math.floor(numri / 10);

if (shifra4 == shifra1 && shifra2 == shifra3)
  alert("Number palindrome");
else
  alert("Number not palindrome");

There could be another approach in which you will compare the string with its reversed value

var numri = window.prompt("Vendosni numrin");
if(numri===numri.split("").reverse().join(""))
  alert("Number palindrome");
else
  alert("Number not palindrome");
void
  • 36,090
  • 8
  • 62
  • 107
  • You could also subtract the previous shifra rather than using floor, eg `numri = (numri - shifra4) / 10` – James Feb 16 '18 at 15:07