Skip to content

Instantly share code, notes, and snippets.

@michael-mafi
Created December 17, 2016 03:42
Show Gist options
  • Select an option

  • Save michael-mafi/8bd8507d17414a2b8262bccb41d1d980 to your computer and use it in GitHub Desktop.

Select an option

Save michael-mafi/8bd8507d17414a2b8262bccb41d1d980 to your computer and use it in GitHub Desktop.
palindrome in JS
function palindrome(str) {
var len = str.length;
for ( var i = 0; i < Math.floor(len/2); i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
///////////////////////////////////////////////////////////another way
var isPalindrome = function (string) {
if (string == string.split('').reverse().join('')) {
alert(string + ' is palindrome.');
}
else {
alert(string + ' is not palindrome.');
}
}
///////////////////////////////////////////////////////////another way
function isPalindrome(s) {
return s == s.split("").reverse().join("");
}
alert(isPalindrome("abba"));
/////////////////////////////////////////////////////////// yet another way
function palindrome(str){
for (var i = 0; i <= str.length; i++){
if (str[i] !== str[str.length - 1 - i]) {
return "The string is not a palindrome";
}
}
return "The string IS a palindrome"
}
palindrome("abcdcba");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment