Palendrome challenge questions

Tell us what’s happening:
I can’t figure out why “almostomla” passes the test I’ve written. Please help!

Your code so far


function palindrome(str) {
let newStr = str.toUpperCase()
let numLetters = []

for (let i=0; i < str.length; i++) {
  if (newStr.charAt(i).match(/[a-zA-Z\d:]/)) {
    numLetters.push(newStr.charAt(i));
  }
}
for (let i=0; i < (numLetters.length); i++) {
  if (numLetters[i] == numLetters[(numLetters.length - 1)-i]) {
    return true
  } else return false
} 
}



palindrome("almostomla");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:75.0) Gecko/20100101 Firefox/75.0.

Challenge: Palindrome Checker

Link to the challenge:

your code returns true as long as a correct match is found. Change your code so that if you find any incorrect match, return false. If all matches are correct, return true.

function palindrome(str) {
let newStr = str.toUpperCase()
let numLetters = []

for (let i=0; i < str.length; i++) {
  if (newStr.charAt(i).match(/[a-zA-Z\d:]/)) {
    numLetters.push(newStr.charAt(i));
  }
}

for (let i=0; i < (numLetters.length); i++) {
  console.log(numLetters[i] +" "+numLetters[(numLetters.length - 1)-i]);
  if (numLetters[i] !== numLetters[(numLetters.length - 1)-i]) {
    return false;
  } 
  
}
return true; 
}
1 Like

this check only the first and last letter

1 Like

Thanks, that makes sense. I appreciate your response!