Build a Palindrome Checker Project

hallo.
i need a help.
I don’t understand why I can’t resolve these two instructions

  • When the #text-input element contains the text 1 eye for of 1 eye. and the #check-btn element is clicked, the #result element should contain the text "1 eye for of 1 eye. is not a palindrome" .

  • When the #text-input element contains the text 0_0 (: /-\ :) 0-0 and the #check-btn element is clicked, the #result element should contain the text "0_0 (: /-\ :) 0-0 is a palindrome" .

I wirte mx conde under.
could you give me a advice

const textInput = document.getElementById("text-input");
const checkButton = document.getElementById("check-btn");
const result = document.getElementById("result");

checkButton.addEventListener("click", () => {
  const textInputValue = textInput.value.trim().toLowerCase().replace(/[^a-zA-Z]/g, "");
  const reverseText = textInputValue.split("").reverse().join("");

  if (textInputValue === "") {
    alert('Please input a value');
} else if (textInputValue === reverseText.replace(/[^a-zA-Z]/g, ""){
  result.innerText = textInput.value + " is a palindrome";
  } else {
    result.innerText = textInput.value + " is not a palindrome";
  }
});

You should be removing all non-alphanumeric characters.
You are removing all non-alphabet characters, which includes numeric characters.

For instance, “1 eye for of 1 eye” is not a palindrome because of the position of the 1 characters.
Your code incorrectly removes the 1 characters, leaving “eyeforofeye”, which is a palindrome.

1 Like

Just as an aside.

else if (textInputValue === reverseText.replace(/[^a-zA-Z]/g, "")

Not sure what happened here but you are missing a closing parenthesis and the one you do have is not the correct type. I assume it is something that happened when you pasted the code.

1 Like

thank you so much! it was sloved!!

1 Like

thank you so much! it was correct! tahnks!!

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