Tell us what’s happening:
Hi so I just finished this cert and was looking for some feedback on perhaps what I could’ve done better since I feel like I probably have unnecessary steps but this is what I got after thinking for awhile.
Your code so far
function palindrome(str) {
var regex = /\W|[_]/g;
//gets rid of any non-alphaNumeric characters
const noPunc = str.replace(regex, '');
//splits the string by letter
let splitStr = noPunc.split('');
//makes it all the smae case
splitStr = splitStr.map(e => e.toLowerCase());
let reverse = [];
//reverses the string
for (let i in splitStr){
reverse.unshift(splitStr[i])
}
//checks if the char placements are the same
const newArr = (reverse.map((e, index, arr) => (splitStr.indexOf(e) === arr.indexOf(e))));
//if everything is the same return true else one false flag means it's not a palindrome.
for (let i in newArr){
if (newArr[i] === false){
return false;
}
}
return true;
}
palindrome("almostomla")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker
Link to the challenge: