Created
December 17, 2016 03:42
-
-
Save michael-mafi/8bd8507d17414a2b8262bccb41d1d980 to your computer and use it in GitHub Desktop.
palindrome in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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