Created
December 9, 2015 17:53
-
-
Save joePichardo/231c7f5cdffd5c3ec98d 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
// Bonfire: Check for Palindromes | |
// Author: @joepichardo | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function palindrome(str) { | |
// Good luck! | |
str = str.toLowerCase(); | |
//replaces spaces, non-alphanumberic, and underscores to no space | |
str = str.replace(/ |_|([^\w]*)/gi,""); | |
//makes a new string so we can compare original and new to find if they are palindromes | |
//puts string into array, reverses, and joins them back together into a string | |
var newString = str; | |
var stringArray = newString.split(""); //splits each letter | |
stringArray.reverse(); | |
newString = stringArray.join("");//joins each letter w/o commas in b/w | |
//used console to check for any missing arguments | |
console.log(str); | |
console.log(newString); | |
console.log(stringArray); | |
//checks if string is a palidrome | |
if(str === newString){ | |
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