JavaScript - Palindrome

Tell us what’s happening:
Describe your issue in detail here.

Hello fellows Campers :wave:,

I am currently doing the Palindrome Checker exercise. I wrote the code below, and it passes most of the check tests. But there are 3 of them that don’t return the good result. I can’t really know how and why, I’ve been searching for a solution but I can’t figure it out. Maybe one of you could tell me what is the problem ?

Thank you for your help ! :smile:

  **Your code so far**
function palindrome(str) {
const nonAlpha = /^[^a-zA-Z0-9]+$/;
str.replace(nonAlpha, "");

const lowerCaseString = str.toLowerCase();

let strMaxIndex = str.length - 1;
let startIndex = 0;
while (startIndex !== strMaxIndex) {
  if (lowerCaseString[startIndex] === lowerCaseString[strMaxIndex]) {
    startIndex += 1;
    strMaxIndex -= 1;
    return true // console.log(str + " is a palindrome !");
  } else if (lowerCaseString.length <= 2) {
   return false // console.log(str + " can't be a palindrome");
  } else {
   return false // console.log(str + " is not a palindrome :(");
  }
}
}

palindrome("eye");
  **Your browser information:**

User Agent is: Chrome/103.0.0.0 Safari/537.36

Challenge: Palindrome Checker

Link to the challenge:

return statement will stop the loop, infact stop the function call and control will return where the function was called

The code doesn’t return the correct answer for str = “_eye”, “almostomla”, and “My age is 0, 0 si ega ym.”

Hello! I’m currently dealing with this challenge too. About _ symbol. Im pretty sure i saw yesterday somewhere that this symbol is considered alphanumeric byJS. I’ll try to find actual link if you need one. My theory is that is the reason for issue with ‘_eye’
Update. I just tested your regexp:

let str1 = "_eye";
const nonAlpha = /^[^a-zA-Z0-9]+$/;
result = str1.replace(nonAlpha, "");
console.log(result)//'_eye'

How many iterations does your loop do?

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