So I’ve completed the “Check for Palindromes” exercise and my solution works. I know there are probably more elegant ways to do this and so I’m simply curious about what you folks may have done differently. This is just a learning exercise to see where I could improve.
Also I could not get the underscore to remove using regular expressions but I’m not sure why so I ended up just using .remove(“_”, ‘’) which worked but if someone could provide some guidance here I’d appreciate it.
function palindrome(str) { // Good luck! var newStr = str;
//function to remove all special characters function replaceSpecial(str){ newStr = str.replace(/(?!\w|\s)./g, '').replace(/\s+/g, '').replace(/^(\s*)([\W\w]*)(\b\s*$)/g, '$2').replace("_", ''); newStr = newStr.toLowerCase(); return newStr; } replaceSpecial(str); //Split new string into an array var strArray = newStr.split(''); //create original check string var orgStr = strArray.join(''); //Reverse the new string into an array var arrRev = strArray.reverse(); //create reversed check string var strRev = arrRev.join(''); //check to see if original string and reversed string are the same if(orgStr === strRev){ return true; } return false; }