DashboardPalindrome number
Back to Forge
Easy CHALLENGE

Palindrome Number

Write a program to check if a given number or string is a palindrome. A palindrome reads the same forwards and backwards.

Logical Approach_

1
Take the input and store it in a temporary variable.
2
Reverse the input using a loop (for numbers) or built-in methods (for strings).
3
Compare the original input with the reversed version.
4
If they match, it is a palindrome; otherwise, it is not.

Variations Hub_

Check if a string (with spaces and punctuation) is a palindrome.
Find the longest palindromic substring in a given string.
Check if a number can be converted to a palindrome by changing exactly one digit.
Implementation Protocol (JS)
function isPalindrome(n) {
  let original = n.toString();
  let reversed = original.split('').reverse().join('');
  return original === reversed;
}

// For numbers without using string conversion
function isNumberPalindrome(num) {
  if (num < 0) return false;
  let original = num;
  let reversed = 0;
  while (num > 0) {
    let digit = num % 10;
    reversed = reversed * 10 + digit;
    num = Math.floor(num / 10);
  }
  return original === reversed;
}

Line-by-Line Breakdown_

Point 1

Line 2: Convert the number to a string to easily manipulate characters.

Point 2

Line 3: Split string into array, reverse array, and join back to string.

Point 3

Line 4: Strict comparison between original and reversed strings.

Point 4

Line 8: Optimized version using mathematical operations to avoid string overhead.

Point 5

Line 11-15: Reverse a number by extracting digits one by one using modulo (%) and building the reversed number.

Ready to run this code?

Execute in the interactive playground for deep analysis.