Why this design does not pass the test?
Tell me what the error is.

In this task you need to write a program that will determine whether the number is a palindrome.

The algorithm is as follows: you need to write the original number back to front and compare this option with the original. If both numbers are equal, we have a palindrome.

var poly = 1221; var ylop = 0; var isPalindrome = false; var biba = 0; while (isPalindrome = false) { biba = poly % 10; ylop = Math.round(poly / 10) + biba.toString(); if (ylop == poly) { isPalindrome = true; } else { break; } } 

Closed due to the fact that off-topic participants Kromster , freim , 0xdb , aleksandr barakin , Edward 13 May at 12:54 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - freim, 0xdb, aleksandr barakin, Edward
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • it seems to me, or your loop never runs, because you isPalindrom = false will always return false ? - ThisMan
  • 3
    and what is the variable biba ?))) funny name - ThisMan
  • one
    and why you did not use the hint, нужно записать изначальное число задом наперёд и сравнить этот вариант с изначальным. your code does something else - ThisMan
  • In addition to all the above, you now take the number 1221 , divide the numbers 122 and 1 into two, after which they are glued together again in 1221 . This is definitely not what the task requires of you. In general, fix isPalindrome == false so isPalindrome == false and try to solve it yourself. - Alex Krass

1 answer 1

If you follow the description that is used in the task, the code should look like this:

 const poly = 13231; const notPoly = 45162; const isPolyndrom = n => { return n.toString() === n.toString().split('').reverse().join(''); } // С учетом что проверяются только целые числа console.log(isPolyndrom(poly)); console.log(isPolyndrom(notPoly)); 

But this is not exactly the best way, because several times we go through all the digits in the number ( split , reverse , join ).
There is a more optimal way:

 const poly = 13231; const notPoly = 45162; const isPolyndrom = n => { const nAsString = n.toString(); const length = nAsString.length; const lastIndex = length - 1; for(let i = 0; i <= Math.round(length / 2); i++) { // Если противоположные элементы относительно центра не равны, то не полиндром if (nAsString[i] !== nAsString[lastIndex -i]) { return false; } } return true; } // С учетом что проверяются только целые числа console.log(isPolyndrom(poly)); console.log(isPolyndrom(notPoly)); 

The advantage is that only one cycle is used.