Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

This is what I have for my code so far and none of the tests except the 3 that are just do you have the right HTML elements. I know that something has to be wrong with my code but I’m not really sure what, maybe the replacing, splitting, or reversing part is whats messing up my code?

Your code so far

/* file: script.js */
const input = document.getElementById("text-input");
const checkButton = document.getElementById("check-btn");
const resultDiv = document.getElementById("result");

function error(){
  alert("Please input a value");
}


function checkPalindrome(){
  if (input.value = ""){
    error()
  } else {
    inputList = input.replace(/^[a-z]/g, "");
    inputList = inputList.split("");
    inputReversed = inputList.reverse();
    if (inputReverse === inputList) {
      resultDiv.innerHTML = `${input} is a palindrome`
    } else {
      resultDiv.innerHTML = `${input} is not a palindrome`
    }
  }
}

checkButton.addEventListener("click", checkPalindrome);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Hello! There’s three things that could potentially be touched to make this work.

Your regex pattern /^[a-z]/g might not be catching everything that should. You can check this by adding a console.log(inputList) just below it, like this:

inputList = input.replace(/^[a-z]/g, "");
console.log(inputList)
inputList = inputList.split("");

Second thing is your inputReverse is an array, and the tests are looking for strings. If you place a consoleLog(inputReverse) just below it, you might see that the output is something like:

[ n ,e, u, q, u, e, n]

You need to find a way to make a string out of this array.

Lastly, in your last if statement, you made a small typo, pretty sure you meant to write “inputReversed” not “inputReverse”. Check that these match.

Good luck!

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