Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

I can not get my else statement to work. I have typed this over so many times and I just don’t see the problem. It is step number 9. I want it to say not a palindrome

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang= "en">
  <head>
    <meta charset= "UTF-8">
    <meta name= "viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />

    <title>Document</title>
    </head>

    <body>
      <input id= "text-input">
      <button id= "check-btn">Check </button>
<div id= "result">
  </div>
  <script src="./script.js"></script>
      </body>  
      </html>
/* file: script.js */
const textInput = document.getElementById("text-input");
const checkButton = document.getElementById("check-btn");
const result = document.getElementById("result");
const replaced = textInput.value.replace(/[^A-Za-z0-9]/g, "")
 
checkButton.addEventListener("click", () => {
  if (textInput.value === ""){
    alert("Please input a value")
  } else if (textInput.value.length === 1) {
    result.innerText = `${textInput.value} is a palindrome`
  } else if (replaced === [...replaced].reverse().join("")){
     result.innerText = `${textInput.value} is a palindrome`
  }  else {
    result.innerText = `${textInput.value} is not a palindrome`
  }

});
/* 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/132.0.0.0 Safari/537.36 Edg/132.0.0.0

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Have you tried to print the replaced variable to the console or the screen?

when I run console.log (“addjkbkajkfakjwei 11 394u920350 >>><<<<”.replace(/[^A-Za-z0-9]/gi, “”)) it works. are you referring to the const statement?

const replaced = textInput.value.replace(/[^A-Za-z0-9]/g, "")
console.log("This is the replaced value:", replaced)

Put it right after you assign the variable. What gets printed out?

Basically want to know what the value stored in the replaced variable is.

it prints out This is the replaced value

What did you put into the text box? What should replaced be?

it looks like the variable is not working . adhfighoiahgio &&&9948349<<<<>>>> is what I put in. and it printed the exact out. but when I run it with just .replace(/[^A-Za-z0-9]/g, “”) it works… ughhh

So, you know the regex works.

Maybe it’s where you assign the variable. It gets assigned when the text box is empty and never updates.

Try moving it somewhere else?

Thank you!!! <3 got it

1 Like

Always print out your variables when you are troubleshooting, otherwise you are flying blind. Confirm the values are what you think they should be.

1 Like