Skip to content
Advertisement

How to check if a string is a palindrome?

I have a code to check whether a word is palindrome or not:

JavaScript

If a word is inputted, for example : rotor , I want the program to check whether this word is palindrome and give output as “The given word is a palindrome”.

But I’m facing problem that, the program checks first r and r and prints “The given word is a palindrome” and then checks o and o and prints “The given word is a palindrome”. It prints the result as many times as it is checking the word.

I want the result to be delivered only once. How to change the code?

Advertisement

Answer

Just reverse the string and compare it against the original

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement