Uses the split(), reverse(), and join() Methods
Another approach, which is through the shortest approach, uses the split(), reverse(), and join() methods.
- Split the string of characters into several different characters (which is though unsorted at the moment).
- Use the reverse() method to reverse all the characters of the string alphabetically.
- Then apply the join() method in order to join all the characters of the string (which are now sorted).
Example: Below is the implementation of the above approach:
// JavaScript code in order to check string palindrome...
let checkPalindrome = (stringg) => {
return stringg === stringg.split("").reverse().join("");
};
console.log("Is Palindrome? : " + checkPalindrome("noon"));
console.log("Is Palindrome?: " + checkPalindrome("apple"));
Output
Is Palindrome? : true Is Palindrome?: false
How to check whether a passed string is palindrome or not in JavaScript ?
In this article, we are given a string, our task is to find string is palindrome or not. A palindrome is a series of numbers, strings, or letters that, when read from right to left and left to right, match each other exactly or produce the same series of characters. in simple words when number strings or characters are inverted and still provide the same outcome as the original numbers or characters.
Example:
Input: "race"
Output: passed string is not a palindrome
Explanation: if we write "race" in reverse that is "ecar" it not
matches with first string so it is not a palindrome.
Input: "hellolleh"
Output: passed string is palindrome.
These are the following methods by which we can check whether a passed string is palindrome or not:
Table of Content
- By using a Naive Approach
- By reversing the string
- Uses the split(), reverse(), and join() Methods