palindrome javascript code example
Example 1: palindrome in javascript
function palindrome(str) {
var len = str.length;
var mid = Math.floor(len/2);
for ( var i = 0; i < mid; i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
Example 2: javascript palindrome check
const isPalindrome = str => str === str.split('').reverse().join('');
isPalindrome('abc');
isPalindrom('abcba');
Example 3: javascript palindrome check
function palindrome(str) {
var re = /[\W_]/g;
var lowRegStr = str.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}
palindrome("A man, a plan, a canal. Panama");
Example 4: palindrome javascript
function palindrome(str) {
var splitted = str.split("");
var reversed = splitted.reverse("");
var joined = reversed.join("");
return joined.toLowerCase().replace(/[^0-9a-z]/gi, '') == str.toLowerCase().replace(/[^0-9a-z]/gi, '')
}
Example 5: javascript palindrome check
function palindrome(str) {
var re = /[^A-Za-z0-9]/g;
str = str.toLowerCase().replace(re, '');
var len = str.length;
for (var i = 0; i < len/2; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
palindrome("A man, a plan, a canal. Panama");
Example 6: palindrome javascript
const reverseString = string.split('').reverse().join('');