Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to check if a number is palindrome recursively in JavaScript

2 Answers

0 votes
function recursivelyCheckPalindrome(n) {
    if (n < 0) {
        return false; 
    }

    // This initializes a static variable inverse_number the first time the function is called. 
    // The use of typeof ensures this initialization only happens once.
    if (typeof recursivelyCheckPalindrome.inverse_number == 'undefined') {
        recursivelyCheckPalindrome.inverse_number = 0;
    }
      
    if (n == 0) {
        return false;
    }
    
    recursivelyCheckPalindrome.inverse_number = (recursivelyCheckPalindrome.inverse_number * 10) + (n % 10);
    recursivelyCheckPalindrome(parseInt(n / 10));
   
    return n === recursivelyCheckPalindrome.inverse_number;
}

const n = 12321;

if (recursivelyCheckPalindrome(n) === true) {
    console.log("Palindrome");
} else {
    console.log("NOT Palindrome");
}



/*
run:
  
Palindrome
           
*/

 



answered Feb 23, 2016 by avibootz
edited Oct 17, 2024 by avibootz
0 votes
function recursivelyCheckPalindrome(n, original) {
    if (n === 0) {
        const originalStr = Math.abs(original).toString();
        
        return originalStr === originalStr.split('').reverse().join('');
    }
 
    return recursivelyCheckPalindrome(Math.floor(n / 10), original);
}
 
const n = 73137;
 
if (recursivelyCheckPalindrome(n, n)) {
    console.log("Palindrome");
} else {
    console.log("NOT Palindrome");
}



/*
run:
  
Palindrome
           
*/

 



answered Oct 17, 2024 by avibootz

Related questions

1 answer 91 views
2 answers 101 views
2 answers 105 views
...