how to make a palindrome checker in 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
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 3: palindrome js
const palindrome = (str) => {
var text = str.replace(/[.,'!?\- \"]/g, "")
return text.search(
new RegExp(
text
.split("")
.reverse()
.join(""),
"i"
)
) !== -1;
}
function palindrome(str) {
let text = str.replace(/[.,'!?\- \"]/g, "").toUpperCase();
for (let i = 0; i > text.length/2; i++) {
if (text.charAt(i) !== text.charAt(text.length - 1 - i)) {
return false;
}
}
return true;
}
console.log(palindrome("Able was I, ere I saw elba."));