Palindrome Checker Help

Hi, I am trying to complete the Palindrome Checker Project.
I wrote my code on VS code, all looks perfect to me. I did all the tests manually and they all work, but freecodecamp still thinks my tests are running wrong.
Any feedback is really appreciated thank you

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Palindrone Checker</title>
  </head>

  <body>
    <h1>Palindrome Checker</h1>
  
    <input type = "text" id = "text-input">
    <button id ="check-btn" onclick = "press()">Press me</button>
    <div id ="result">Results Div</div>

    <script src="script.js"></script>
  
  </body>
</html>
type or paste code here

javascript:

const textInput = document.querySelector("input");
let filterInput = textInput.value;
let textLength = filterInput.length;

let resultEl = document.getElementById("result")


textInput.addEventListener("keyup", () => {
    filterInput = textInput.value.replace(/[^A-Z0-9,. ]/ig, "");
    textLength = filterInput.length
    filterInput = filterInput.toLowerCase();
});


function press() {

    if (textLength === 0) {
        alert ("Please input a value")
    } else {
        check()

    }
}


function reverseString(filterInput) {
    var splitString = filterInput.split("");
    var reverseArray = splitString.reverse();
    var joinArray = reverseArray.join("");

    return joinArray
}

function check() {
    let reverseEl = (reverseString(filterInput))
    console.log(filterInput)
    console.log(reverseEl)

    if (filterInput === reverseEl) {
        console.log("Palindrome")
        resultEl.textContent = (textInput.value + " is a palindrome");
    } else {
        console.log("Not a Palindrome")
        resultEl.textContent = (textInput.value + " is not a palidrome");
    }

}

The first problem is that this is not being triggered by the tests. I would put this logic with your press function

1 Like

Thank you so much for your reply, so I tried putting everything into the “press” function.
All tests are working now, except for when its not a palindrome. It still says its not passing.
Really not sure why :confused:

edit: I think its because of the else if, and then the final else condition, if I make a “check function” that does the whole check it wont pass the tests if its a palindrome or not

let resultEl = document.getElementById("result")

function press() {
    let textInput = document.querySelector("input");
    let filterInput = textInput.value.replace(/[\\,_.0-9 ]/ig, "");
    filterInput = filterInput.toLowerCase()
    let textLength = filterInput.length;
    console.log(textLength)

    let reverseEl = (reverseString(filterInput))
    console.log(filterInput)
    console.log(reverseEl)
    if (textLength === 0) {
        alert ("Please input a value")
    } else if (filterInput === reverseEl) {
        resultEl.textContent = (textInput.value + " is a palindrome");
        console.log("Palindrome")
    } else {
        resultEl.textContent = (textInput.value + " is not a palidrome");
        console.log("Not a palindrome")
    }
}


function reverseString(filterInput) {
    var splitString = filterInput.split("");
    var reverseArray = splitString.reverse();
    var joinArray = reverseArray.join("");

    return joinArray
}

You have a typo here.


This will remove numbers. but, if you re-read the note at the challenge again:

You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)

It says non-alphanumeric so you need to keep numbers also.


Your code should now pass all tests except for one…
0_0 (: /-\ :) 0-0

back to your regex /[\\,_.0-9 ]/ig
(after you edit it to keep numbers)

you will see that you are not removing : , -, / and parentheses (). so you can add them manually like what you did, or think of a better way to do it.

Think about character classes like \d for example

1 Like

Thank you so much, I need to look closer!

I’ve fixed the typo and fixed the regex to include numbers and to remove the : , - , /
Code is finally passing all tests :star_struck: :star_struck:

1 Like