JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Hi! I´m trying to do the Palindrome Challenge. I don´t understand why the result string always has “undefined” at the beggining.

Your code so far

function palindrome(str) {
  let regex = /\w+/gi;
  let noSpaces = str.match(regex); 
  let newStr = []; 
  let result = [];
  for (let word in noSpaces){
    newStr += noSpaces[word];
  }
  for (let i = newStr.length; i >= 0; i--){
    result += newStr[i];
  } 
  console.log(result) /* **this returns "undefinedeye"** */;
  if (result === noSpaces){
    return true
  } else {
    return false
  } 
} 

console.log(palindrome("eye"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

Yes, but I thought of doing let i = newStr.length + 1; but it didnt´t work? Maybe i didn´t do it correctly

Ohhhh, you´re rigth! Thank you!

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