Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

i believe my code are nonsense, like there should be shorter way to write this, using methods..i just want to know if i am on the right path?

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>palindromes</title>
  </head>
  <body>
    <input id="text-input">
    <button id="check-btn">check</button>
    <div><span id="result"></span></div>

    <script src="script.js"></script>
  </body>



</html>
/* file: script.js */
let textInput = document.getElementById("text-input");
const button = document.getElementById("check-btn");
const result = document.getElementById("result");
button.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 (textInput.value === "eye"){
    result.innerText = "eye is a palindrome";
  }else if (textInput.value === "_eye"){
    result.innerText = "_eye is a palindrome";
  }else if (textInput.value === "race car"){
    result.innerText = "race car is a palindrome";
  }else if (textInput.value === "not a palindrome"){
    result.innerText = "not a palindrome is not a palindrome.";
  }else if (textInput.value === "A man, a plan, a canal. Panama"){
    result.innerText = `${textInput.value} is a palindrome`;
  }else if (textInput.value === "never odd or even"){
    result.innerText = `${textInput.value} is a Palindrome`;
  }else if(textInput.value === "nope") {
    result.innerText = `${textInput.value} is not a palindrome `
  } else if (textInput.value === "almostomla"){
  result.innerText = `${textInput.value} is not a palindrome`
  } else if (textInput.value === "My age is 0, 0 si ega ym."){
  result.innerText = `${textInput.value} is a palindrome`
  }else if (textInput.value === "1 eye for of 1 eye."){
    result.innerText = `${textInput.value} is not a palindrome`
  }else if (textInput.value === "0_0 (: /-\ :) 0-0"){
    result.innerText = `${textInput.value} is a palindrome`
  }else if (textInput.value === "five|\_/|four"){
    result.innerText = `${textInput.value} is not a palindrome`

  }
})

/* file: styles.css */
#check-btn{

}

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 OPR/117.0.0.0

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Right now, your code looks specifically for the examples that freeCodeCamp checks for when you test your code. While that might allow your code to pass the criteria and gives you what you need for your certification, it does miss the point of the projects required for your certification. These projects give you the opportunity to work through the solution on your own. As a programmer, you’ll often need to break down the problem into the individual steps that lead you to your solution.

For this project specifically, you should be able to enter anything into the input and the code finds out if it’s a palindrome or not. How can we break this problem down into the steps needed to get us there?

1 Like

It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?

To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: Hard-coding For Beginners

Let us know if you have a question about how to make your code more flexible.

1 Like

Thank you I will stop for now and go study more from the link you sent.God bless :folded_hands:t6:

Thank you so much :folded_hands:t6:

1 Like

For sure! If you’d like to, let us know what your thought process is for solving the problem step by step, and we can help by giving some feedback on it. There’s often more than one way to approach building code, and I’d love to see your method!

1 Like

Yess thank you, i was thinking what if I give, the text input a method of reverse, join and negative regex of all alphabets and numbers..but with the purpose to take out whatever else that isn’t. Which I belive will clean up the text input. Then put text input in a condition of if which states “if text input is true then its a palindrome or else it is not”

But I have a question ? If I use the reverse and join method does this means JavaScript automatically sees any texts reversed and check if they are still spelt the same way even when reversed ? Or does it just reverse the word and anything goes ? Even if the text is not palindrome?

there is no automatically in JavaScript, you would need to write the comparison between the reversed string and the original string

1 Like