JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Tell us what’s happening:
I’m trying to solve this using Stack DS but I’m getting an error.
palindrome(“0_0 (: /-\ :slight_smile: 0-0”) should return true.
Can someone tell me how to fix my code?

function palindrome(str) {
const regex = new RegExp("[^a-zA-Z0-9\s-]", "g");
let word = str.replace(regex, "").toLowerCase();
let rword = "";
var letters = [];
for (var i = 0; i < word.length; i++) {
  letters.push(word[i]);
}

// pop off the stack in reverse order
for (var i = 0; i < word.length; i++) {
  rword += letters.pop();
}

if (rword === word) {
  return true;
}
else {
  return false;
}
}
palindrome("eye");
  **Your browser information:**

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

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

Try to log what is in this variable after you use the regex to clean it up. Is it 0000?

No, it’s eye.
js
console.log(word);
eye

I mean you should add a console.log in the function….

Right now you are not logging inside the function.

You mean after line 3?
I tried console.log(word); there and it showed eye in the console.

Yes please.

Then you need to modify the test line(the last line) to give the string that is failing the fCC test…

So


palindrome(“0_0 (: /-\ :) 0-0”) 

It’s showing 00-0-0
The last line was palindrome(“0_0 (: /-\ :slight_smile: 0-0”);

So do you see the problem that you should fix now?

1 Like

Yeah, it worked after changing the regex and removing ‘-’
Thanks

1 Like

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