Palindrome Checker -solution Feedback

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:

First, congrats on getting it done.

I think your solution may be just slightly more complex than it needs to be. You definitely can go through the letters individually in each string, comparing them to see if they always match. But remember, you can also just compare entire strings using the === operator. For example:

console.log('Test' === 'Test');

This is going to print true to the console.

Perhaps that will give you an idea about how you can simplify your code just a little?

Thanks I changed it and it still worked.I tried to do something like that earlier with it but I think I wasn’t reversing the string correctly so it wasn’t working properly. I think because of that I just forgot I could do it or just thought it wouldn’t work. Anyway thank you for the feedback.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.