Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

javascript for-loop

let num = 343
let temp=num
let value=0
while (num > 0) {
  let rem= num%10
  value = value*10 + rem
  num=num/10
}

if(temp == value){
  console.log(`${temp} is a Palindrome No`)
}
else
  console.log('it is not a palindrome no.');

output is always shown as not a palindrome no.

1 Answer

Steven Parker
Steven Parker
243,266 Points

When you divide by 10, "num" gets smaller but will continue to be larger than 0 until the floating-point precision is exhausted. So the loop runs many times, and "value" becomes a very large number.

What you probably intended is to get only the full integer result of the division without the fractional part. This can be done with the "floor" function of the Math library:

  num = Math.floor(num / 10);

thats right , the code is running correct now