Last active
April 9, 2019 12:55
-
-
Save Christonja/4ce34477f852a2c6ede10c9fee6e4601 to your computer and use it in GitHub Desktop.
Palindrome Checker created by Christonja - https://repl.it/@Christonja/Palindrome-Checker
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
/*JavaScript Algorithms and Data Structures Projects: Palindrome Checker: | |
Challenge designed by FreeCodeCamp, solution derived completely from my learning obtained | |
thanks to FreeCodeCamp and other sources on the internet, notably W3Schools.com, function calls | |
provided by FreeCodeCamp as ways to test the algorithm*/ | |
function palindrome(str) { | |
// Good luck! | |
//Create a new regexp for checking for alphanumeric characters in string | |
var regexp = /[0-9a-zA-Z]+/g; | |
//Split every character of string into it's own array element | |
var tempArr = str.split(""); | |
//Filter the new array based on if it matches the regexp created earlier, if it does, place into the new variable strArr | |
var strArr = tempArr.filter(arr => arr.match(regexp)) | |
//Create a newStrArr and then push all strArr elements onto this newStrArr in reverse order | |
var newStrArr = []; | |
for (var i = strArr.length-1; i >= 0; i--) { | |
newStrArr.push(strArr[i]); | |
} | |
//Join up the newStrArr back into a string and conver to lower case | |
var reverseStr = newStrArr.join("").toLowerCase(); | |
//Create a finalstr from the original str that corresponds with the lowercase formatting and alphanumeric characters of the reversestr | |
var finalStr = str.split(/[^A-Za-z0-9]/).join("").toLowerCase(); | |
//Log the reverse and final strings for purpose of debugging and as a visual console representation | |
console.log("Reverse String: " + reverseStr + " | " + "Original String: " + finalStr); | |
//Perform the check if strings equal then the input string is indeed a palindrome, otherwise return false | |
if (reverseStr === finalStr) { | |
return true | |
} | |
return false; | |
} | |
palindrome("eye") //should return a boolean. | |
palindrome("eye") //should return true. | |
palindrome("_eye") //should return true. | |
palindrome("race car") //should return true. | |
palindrome("not a palindrome") //should return false. | |
palindrome("A man, a plan, a canal. Panama") //should return true. | |
palindrome("never odd or even") //should return true. | |
palindrome("nope") //should return false. | |
palindrome("almostomla") //should return false. | |
palindrome("My age is 0, 0 si ega ym.") //should return true. | |
palindrome("1 eye for of 1 eye.") //should return false. | |
palindrome("0_0 (: /-\ :) 0-0") //should return true. | |
palindrome("five|\_/|four") //should return false. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment