Build a Palindrome Checker - Build a Palindrome Checker

Tell us what’s happening:

This lab is tricky. I’m failing tests 7 8 10 11 14 16
Is there something wrong with my regex?

Thank you,
Good day,
Mike

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Palindrome Checker</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
  <input id="text-input">
  <hr>
  <button id="check-btn">Check for Palindrome</button>
  <p id="result"></p>
  <script src="script.js"></script>
</body>

</html>

/* file: styles.css */

/* file: script.js */
const checker = document.getElementById("check-btn");
const userInput = document.getElementById("text-input");
const result = document.getElementById("result");
const regex = /^\W/gi;
const value = userInput.value
checker.addEventListener("click", ()=> {
  const input = userInput.value.replace(regex,"").toLowerCase();
  const reversed = input.split("").reverse().join("");
  if (input===""){
    alert("Please input a value");
  }
  else if (reversed === input){
    result.textContent=`${input} is a palindrome`;
  } else {
    result.textContent=`${input} is not a palindrome`;
  }
});

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15

Challenge Information:

Build a Palindrome Checker - Build a Palindrome Checker

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-palindrome-checker/657bdc55a322aae1eac3838f.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @MChance,

The first thing I notice when I test in the preview window with “A” is that you lose the case.

And it looks like “race car” fails because you haven’t implemented this instruction:

Note: You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)

Your regular expression pattern is not doing that. Try testing with this Regular Expression Tester.

Should you assign your value variable outside of the listener? Is there anything in the userInput field before the listener is triggered?

Happy coding

Thank you so much @dhess

Indeed the regex was very wrong and I had a few configurations that were unaccounted for. That’s actually one of my main weaknesses in coding : there is always 1 or 2 configurations I forget :sweat_smile:

Thanks again

Happy coding!!