Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

I have tried everything but my code will not work it has been 3 days I have even learned regex from scratch but nothing works for is not a palindrome. everything is reflected as a palindrome.

Your code so far

<!-- file: index.html -->
/* file: script.js */

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

const cleanText = textInput.value
      .trim()
      .toLowerCase()
      .replace(/[\W]+/g, "");


const palindromeChecker = () => {
  if(textInput?.value.length > 0) {
    isPalindrome();
  } else {
    noText();
    result.textContent="";
  };
}

function noText() {
  if(textInput?.value.length===0) {
    alert (
      "Please input a value");
    } 
};

const isPalindrome = () => {
  if (textInput?.value.length === 1) {
    return result.textContent = `${textInput?.value} is a palindrome`;
  } else if (cleanText === cleanText.split("").reverse().join("")) {
      result.textContent = `${textInput.value} is a palindrome`;
    } else {
      result.textContent = `${textInput.value} is not palindrome`;
    }
  };

checkButton.onclick = palindromeChecker;
/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

I can’t enjoy my weekend until i find some clarity on this.

someone please help me I don’t have any hope but the forum sana because wow

guys can someone please help

i wish someone with a good heart and a great understanding of regex can help.

i don’t know how many youtube videos I have watched to sort this code but everything is always a palindrome and I think there is something tragic with my conditions.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

You should not be checking the value so early. If you add a console log statement to check what textInput.value is here you will probably not see what you are typing.

Instead get the value after the user clicks (in which ever function is triggered then)

As for giving you help. As this is a certificate project, you must ask very specific questions first to get targeted help.

Hi there, I have edited my code but nothing seems to work still.

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

const cleanText = textInput.value.replace(/\W_/g, "");

const lowered = cleanText.toLowerCase();

const reversedText = lowered.split("").reverse().join("");

const isPalindrome = () => {
if(textInput.value ==="") {
alert (
"Please input a value");
} else if(lowered === reversedText) {
result.textContent = `${textInput.value} is a palindrome`;} else if(lowered !== reversedText ) { result.textContent = `${textInput.value} is not palindrome`;
}

};

checkButton.onclick = isPalindrome

you’re still trying to check the value too early.
You should not be doing that until the event or click is triggered

1 Like

I am so sorry for being such a nuisance, and thank you for being so patient with me. I really appreciate your help, God bless you. I hace done what you advised now my code work but it does not pass the test. here is the updated code:

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


const isPalindrome = () => {

const cleanText = textInput.value.trim()replace(/[\W_]+/g, "").toLowerCase();
const reversedText = cleanText.split("").reverse().join("");

if(textInput.value ==="") {
alert (
"Please input a value");
} else if(cleanText === reversedText) {
result.textContent = `${textInput.value} is a palindrome`;
} else if(cleanText !== reversedText ) {
 result.textContent = `${textInput.value} is not palindrome`;
}

};

checkButton.addEventListener("click", isPalindrome());

1 Like

I realised I had a typo after I took your advise. “not palindrome” instead of “not a palindrome”. you have been the best ever! if only I could thank you in person which is not likely. You deserve it ! Not all heros wear capes.

no problem, but remember, if you need help with a certification project, you need to ask a specific question (one that shows what you have tried to do for eg and talks about what you think is the issue)

1 Like