Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

Hi, I’m trying to run the test in my project but when I click the button nothing happens, I’m using Microsoft Edge. I don’t know if there is an error in my project or the code is not uploading.

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">
  <title>Palindrome Checker</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <figure>
      <img src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" alt="Logo freeCodeCamp">
    </figure>
    <h1>Is it a Palindrome?</h1>
    <div class="container-input">
      <form onsubmit="return false;">
        <label for="text-input">Enter in text to check for a palindrome:</label>
        <input type="text" id="text-input"><button type="submit" id="check-btn">Check</button>
      </form>
      <div id="result"></div>
    </div>
    <div class="container-info">
      <p><span>💡</span>A <i>palindrome</i> is a word or sentence that's spelled the same way both foward and backward, ignoring punctuation, case, and spacing.</p>
    </div>
  </div>
  <script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
*, ::after, ::before {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
:root {
  --background-color: #0a0a23;
  --text-light: #fff;
  --text-dark: #0a0a23;
  --color-blue: #002ead;
}

body {
  font-size: 62.5%;
  background-color: var(--background-color);
  color: var(--text-light);
  width: 100%;
  height: 100vh;
  display: grid;
  place-content: center;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
}
img {width: 280px;}
h1 {font-size: 2.6rem;}
label {display: block; font-size: 1.2rem;}

.container {
  width: 450px;
  text-align: center;
  display: flex;
  flex-direction: column;
  row-gap: 1.6rem;
}
.container-input {
  background-color: white;
  color: var(--text-dark);
  width: 100%;
  border-radius: 20px;
  font-size: 1rem;
  padding: 1.5rem 1rem;
  box-shadow: 0px 6px 0px 0px #012791;
}
#text-input {
  text-align: center;
  border: none;
  border-bottom: 2px solid var(--color-blue);
  padding: 5px;
  width: 60%;
  font-size: 1.2rem;
  margin: 2rem 0;
}
#check-btn {
  background-color: var(--color-blue);
  color: var(--text-light);
  border-radius: 30px;
  padding: 12px 28px;
  border: none;
  margin-left: 1rem;
  cursor: pointer;
}
#check-btn:hover {
  background-color: #022482;
}
.container-info {
  background-color: #00471b;
  font-size: 1.3rem;
  padding: 1rem;
  border-radius: 20px;
}
.text-result {
  font-size: 1.4rem;
}
/* file: script.js */
const textInput = document.getElementById('text-input');
const checkButton = document.getElementById('check-btn');
const result = document.getElementById('result');
RegExp = /[^a-zA-Z0-9]/gi;

const textInputCleanFunction = (input) => {
  const inputClean = input.toLowerCase().replace(RegExp, "");
  isPalindrome(inputClean);
};
const isPalindrome = (input) => {
  const inputNormal = input;
  const inputReverse = input.split("").reverse().join("");

  if (inputNormal === inputReverse) {
    result.innerHTML = `<p class="text-result"><strong>${textInput.value}</strong> is a palindrome.`;
  } else {
    result.innerHTML = `<p class="text-result"><strong>${textInput.value}</strong> is not a palindrome.`;
  }
  textInput.value = "";
};

checkButton.addEventListener('click', () => {
  textInput.value ? textInputCleanFunction(textInput.value) : alert('Please input a value.');
});

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

you should try putting console.log(“testing line x”) at different lines of the program to see where it is stopping.

1 Like

Hello!

This is the culprit that stopped your app from working. If you change “RegExp” into a simple const variable, all tests pass! Remember to correct replace() as well!
I discovered the bug in the Chrome dev tool console, RegExp was interpreted as a faulty constructor.

2 Likes

Thank you! Can you explain why this happens?

1 Like

Just syntax errors.

This works too (found on Stack Overflow):
const reg = new RegExp(/[^\a-zA-Z0-9]/gi);

1 Like