javascript function to check if a string is a palindrome 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: js palindrome number
isPalindrome = function(x) {
if (x < 0) {
return false;
}
return x === reversedInteger(x, 0);
};
reversedInteger = function(x, reversed) {
if(x<=0) return reversed;
reversed = (reversed * 10) + (x % 10);
x = Math.floor(x / 10);
return reversedInteger(x, reversed);
};