JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Tell us what’s happening:
Hi!
For all palindromes that is true it works and returns ‘true’ but for the ones were I am supposed to receive ‘false’ it does not work.
Is there an issue with my for and if statements or why does it not return false when it is not a match?

Thank you in advance!

Your code so far

function palindrome(str) {
let noNumRegex = /[a-zA-Z0-9]/gi;

let newArr = str.match(noNumRegex);
console.log(newArr);

let switchedArr = newArr.reverse();
console.log(switchedArr);

for (let i = 0; i < newArr.length; i++) {
   if (newArr[i] !== switchedArr[i]){
      return false;
} 
}
  return true;
} 

palindrome("eye");
palindrome("nope");
palindrome("five|\_/|four");




Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

hello and welcome to fcc forum :slight_smile:

  • put a “console.log” in this if block and check what (string/character) are you “checking in this block”
  • fyi, without this “code snippet” you will get same output as you are currently getting with it
  • rethink this code block, im sure you will figure something out

happy coding :slight_smile:

2 Likes

Thank you for the response and the tips! I figured it out and everything works! :smiley:

1 Like