Last active
October 10, 2018 04:41
-
-
Save Nicknyr/17e84cc77b74e7dee351d7dfa298b83f to your computer and use it in GitHub Desktop.
Javascript Palindrone - Turn string into array and then use .reverse method to compare
This file contains 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 isPalindrone(word) { | |
// This approach converts the string into an array and then reverses it | |
// Split splits the string into an array, each letter of the word is it's own array element | |
// Reverse reverses the array elements but they are still separate characters spaced out | |
// Join squishes the letters back into be one solid word without spaces between characters | |
let reversed = word.split('').reverse().join(''); | |
// Compare orginal word to reversed word | |
if( word == reversed) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
console.log(isPalindrone('racecar')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment