Created
November 10, 2017 09:06
-
-
Save VadimKlimets/cd67f605c7ea6d05e71757995ca86491 to your computer and use it in GitHub Desktop.
Check for Palindromes
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 palindrome(str) { | |
// Good luck! | |
//Empty array | |
var array1 = []; | |
// alert("array1 is an " + typeof(array1)); | |
//Empty array | |
var array2 = []; | |
// alert("array2 is an " + typeof(array2)); | |
//Turns all uppercase symbols in the string to lowercase symbols; array turns into string type | |
array1 = str.toLowerCase(); | |
// alert("array1 is a " + typeof(array1)); | |
//Removes all ("g" means "globally") non-alphanumeric symbols("W+"")(or replacing them with ""); | |
//array1 turns into string type | |
array1 = array1.replace(/\W+/g,""); | |
// alert("array1 is a " + typeof(array1)); | |
//Removes all ("g" means "globally") underscores (or replacing them with ""); | |
//array1 turns into string type | |
array1 = array1.replace(/_/g,""); | |
// alert("array1 is a " + typeof(array1)); | |
//Splits string into the array1, reverse elements in the array1, joins elements of the array1 into the string | |
array1 = array1.split("").reverse().join(""); | |
// alert("array1 is a " + typeof(array1)); | |
//Splits string into the array2, reverse elements in the array2, joins elements of the array2 into the string | |
array2 = array1.split("").reverse().join(""); | |
// alert("array2 is a " + typeof(array2)); | |
//COMPARES array1 AND array2. IF THEY ARE STRICTLY EQUAL, THEN THE "str" IS A PALINDROME. | |
//IF THEY ARE NOT STRICTLY EQUAL, THEN THE "str" IS NOT A PALINDROME. | |
if (array1 === array2) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
palindrome("eye"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment