Solving Palindrome Number Problem in JavaScript

Solving Palindrome Number Problem in JavaScript

Another JavaScript algorithm solution, we will understand how to check if a number is a palindrome number or not. I know the next question that comes to mind is what exactly is the word palindrome number? No need to worry I will give you a clear explanation and afterward you will know how to solve this problem in your own preferred programming language. Let’s go.

A palindrome number is a number that reads the same backward as forward. For example, the numbers 121 and 525 is a palindrome because if we reverse the digit backward we will still get 121 and 525 respectively.

Now that we understand what a palindrome number is how about we learn how to solve this challenge using JavaScript which is my preferred programming language.

First, we will need to write a function that checks if a given number is a palindrome inside the function we can convert the number into a string

const isPalindrome =  (numb) => {
    const convertToString  = numb.toString();
}

Then we will reverse the number we just converted backward. To do that we first split the string into an array, we can then reverse the array, then we join the reversed array into a string back

const isPalindrome =  (numb) => {
    const convertToString  = numb.toString();
    const reverseNumb = convertToString.split("").reverse().join("")
}

After reversing the string backward, we can now check if the converted string (Forwards) is the same as the reversed string (Backward)

const isPalindrome =  (numb) => {
    const convertToString  = numb.toString();
    const reverseNumb = convertToString.split("").reverse().join("")
    if (reverseNumb === convertToString  ){
        return true
    } else {
        return false
    }
}

We have to put into consideration that negative numbers cannot be palindrome numbers because the challenge asked us to determine if a given number is a palindrome or not we have to put it in mind that only positive numbers can be a palindrome. To tackle this we will use this function to solve the palindrome number challenge.

Const isPalindromeNumber = (numb) => {
    if(numb < 1){
        return false 
    } else {
        return isPalindrome(numb)
    }
}

Console.log(isPalindromeNumber(121))

Now we’ve learned that a palindrome number is a number that reads the same backward as forward. We have also learned how to this challenge using JavaScript. We can now easily explain and solve this problem anywhere. Happy Coding.