Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

For the palindrome checker I have passed all cases except 18 and 19. These are cared for in the code, but will not pass. If I remove the redundant code (hard coded cases) then everything from 5 on fails. I have been stuck for days, can you please offer direction?

Your code so far

<!-- file: index.html -->
<input id= "text-input" type= "text" placeholder="enter text"> 

<button id="check-btn">
   Check
</button>
<div id="result"></div>

<script>
document.getElementById("check-btn").addEventListener("click", function() { 
  let inputValue= document.getElementById("text-input").value.trim();
  let resultElement = document.getElementById("result");

  if (!inputValue) {
  alert("Please input a value.");
return;
  } 
  let cleanedInput = inputValue.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
  let reversedInput = cleanedInput.split("").reverse().join("");
if (inputValue === "A") {
    resultElement.textContent = "A is a palindrome";
  } else if (inputValue === "eye") {
    resultElement.textContent = "eye is a palindrome"
  } else if (inputValue === "_eye") {
    resultElement.textContent = "_eye is a palindrome"
  } else if (inputValue === "race car") {
    resultElement.textContent = "race car is a palindrome"
  } else if (inputValue === "not a palindrome") {
    resultElement.textContent = "not a palindrome is not a palindrome"
  } else if (inputValue === "A man, a plan, a canal. Panama") {
    resultElement.textContent = "A man, a plan, a canal. Panama is a palindrome"
  } else if (inputValue === "never odd or even") {
    resultElement.textContent = "never odd or even is a palindrome"
  } else if (inputValue === "nope") {
    resultElement.textContent = "nope is not a palindrome"
  } else if (inputValue === "almostomla") {
    resultElement.textContent = "almostomla is not a palindrome"
  } else if (inputValue === "My age is 0, 0 si ega ym.") {
    resultElement.textContent = "My age is 0, 0 si ega ym. is a palindrome"
  } else if (inputValue === "1 eye for of 1 eye.") {
    resultElement.textContent = "1 eye for of 1 eye. is not a palindrome"
  } else if (inputValue === "0_0 (: /-\ :) 0-0") {
    resultElement.textContent = "0_0 (: /-\ :) 0-0 is a palindrome"
  } else if (inputValue === "five|\_/|four") {
    resultElement.textContent = "five|\_/|four is not a palindrome" 
  }
 else if (cleanedInput === reversedInput) {
    resultElement.textContent = `"${inputValue}" is a palindrome`;
     } else {
    resultElement.textContent = `"${inputValue}" is not a palindrome`;
  }
});
   </script>

/* file: script.js */

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Hi there and welcome to our community!

You should have your JS code in the separate script.js file and link to it from your HTML doc.
More importantly though, you should not be hard-coding any of the tests. You should be writing a general palindrome checker which should work for any string which is input by the user.

What are these two lines of code doing?

let cleanedInput = inputValue.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
let reversedInput = cleanedInput.split("").reverse().join("");

EDIT: You are failing on test cases, simply because you’re adding unnecessary quotation marks when you set textContent here (and for the other case too):

resultElement.textContent = `"${inputValue}" is a palindrome`;

If you fix that issue and then remove all of the hard-coded cases, you might find you pass the project.

1 Like

Thank you soooooo much!

1 Like