LearnFrenzy
Sign in Take a free test

Loops · Loops Coding Problems · Question 6

Palindrome numbers in a range in JavaScript Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

Print all palindrome numbers between 100 and 200 by reversing each number with a loop (no strings).

Solution
main.js
function reverseNumber(n) {
  let rev = 0;
  while (n > 0) {
    rev = rev * 10 + (n % 10);
    n = Math.floor(n / 10);
  }
  return rev;
}

const palindromes = [];
for (let n = 100; n <= 200; n++) {
  if (reverseNumber(n) === n) palindromes.push(n);
}
console.log(palindromes.join(" "));

Output Verified by running it on Node.js

101 111 121 131 141 151 161 171 181 191

A palindrome reads the same reversed. Building the reverse digit by digit avoids string conversion; 200 reversed is 2, so it is excluded.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links