Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

Taking the tasks one by one:
When you click on the #check-btn element without entering a value into the #text-input element, an alert should appear with the text “Please input a value”?
I am a bit confused as to what comes first, i tried to declare some variables in the global scope to use them in a function to check text input but not working…

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>
      </title>
      <link rel="stylesheet" href="styles.css">
      <script src="scripts.js"></script>
  </head>
  <body>
    
    <img src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" class="image">
    <h1> Is it a palindrome ?</h1>

    <div id="result">

  <label id="for_text" class="for_text"> Enter a text to check for a palindrome:
        </label>
<input type="text" id="text-input">

<button id="check-btn" onclick="certInput()">Check</button>   
      </div>
      
      <p>
         A <i>palindrome</i> is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing
        </p>
    
    </body>
  </html>
/* file: styles.css */
body { 
  background-color : black;
}
h1, p {
  color: white;
  text-align: center;
  vertical-align: center;
}
h1 {
  text-align: center;

}
img {
  width: 300px;
  height: 200px;
  margin: auto;
  align: center;
   
  
}
input {
  display: flex;
  width: 200px;
  height: 30px;
}
button {
  display: flex;
  width: 100px;
  height: 30px;
  color: white;
  background-color: purple;
  border-radius: 20px;
  text-align: center;
  justify-content: center;
}

div {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
  height: 120px;
  gap: 10px;
  background-color: #FFFFFF;
  margin: auto;
  justify-content: center;
  border-radius: 20px;
}
p {
  background-color: green;
}
label {
  word-spacing: 5px;
  padding-top: 5px;
}
/* file: script.js */
const btn = document.getElementById("check-btn");

const text = document.getElementById("text-input");

const rslt = document.getElementById("result");

btn.addEventListener("click", certInput());
function certInput() {
  if (text = "" || null ) {
    alert("Please input a value")
  } 
  else {checkPalindrome()}
}

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 Edg/126.0.0.0

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Hi @Abycode

Your JavaScript file is not linked to the html page, as the file name is not correct.

Also, place the script element above the body closing tag, otherwise the JavaScript will load before the html.

The if condition is assigning an empty string to the text variable.
Try console.log on the text variable to check if the output is correct.

Happy coding

Thank you for your feedback, i made recommended changes with no effect yet.
<script src="script.js"></script> above the </body>, and changed return for console.log(“Please input a value”).

I modified your last comment so we can read the code you included. Inline html must be placed in backticks to be readable on the forum.

In your script, this code will not add an event listener because you are giving the function a second argument which is not a callback function. To correct it, remove the () parenthesis next to the function certInput

Thank you, updated code below, still not rendering!

const btn = document.getElementById("check-btn");

const text = document.getElementById("text-input");

const rslt = document.getElementById("result");

btn.addEventListener("click", certInput);
function certInput() {
  if (text === "" || null ) {
    console.log("Please input a value")
  } else {
  }
}

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 (').

1 Like

Pls remove the onclick property here if you want to use the event listener code in js.

1 Like

Thank you, i tried this but still can’t render on my project, could something else be fundamentally wrong?

Can you post all the code again in full here?

Also this looks wrong because text is an element not a string.

1 Like

Html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>
      </title>
      <link rel="stylesheet" href="styles.css">
      
  </head>
  <body>
    
    <img src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" class="image">
    <h1> Is it a palindrome ?</h1>

    <div id="result">

  <label id="for_text" class="for_text"> Enter a text to check for a palindrome:
        </label>
<input type="text" id="text-input">

<button id="check-btn">Check</button>   

      </div>
      
      <p>
         A <i>palindrome</i> is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing
        </p>
    
    <script src="script.js">
    </script>
    </body>
  </html>

Javascript:

const btn = document.getElementById("check-btn");

const text = document.getElementById("text-input");

const rslt = document.getElementById("result");

btn.addEventListener("click", certInput);

function certInput() {
  if (text ="" || null ) {
    console.log("Please input a value")
  } 
  else {}
}

what are you expecting this code to do that it is not doing at the moment?

this statement is trying to assign blank string to the text variable, just fyi.

This feedback helped my thought, i was trying to target the text input first with the mistake of using a const when in fact the value could change, so i had to use a let to declare a text.value variable , that sorted me the room to equate to “” 0r null , then to return “Please input a value”. Thank you.

make sure though you do not put that let statement in the global scope!

1 Like

this will end up failing the tests.
You should never get the value in the global scope.

1 Like

this is better. Can you remove the code though to allow others to read the thread without spoilers?

1 Like