Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

When the #text-input element contains an alphanumeric palindrome, the #result element should correctly identify it as a palindrome

This is where I am stuck, I created a regex and passed it on my else if statement but it’s not working. Please help me out

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="eng">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> Palindrome Checker Project</title>
    <link rel="stylesheet" href="styles.css">
    
  </head>
  <body>
    <p>ChinazaEkpere</p>
    <h1>Is it a Palindrome?</h1>
    <div class="input-div">
      <label for="text-input" id="label">Enter in text to check for a palindrome:</label><br>
      <input id="text-input" >
      <button id="check-btn" >Check</button>
      <span id="result"></span>
    </div>

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

const checkBtn = document.getElementById("check-btn");
const textInput = document.getElementById('text-input');
const resultElement = document.getElementById('result');
const alphanumericPal = /[^a-z0-9]+s/;
checkBtn.addEventListener("click", checkButton);
  

function checkButton(){
  if(textInput.value === ''){
    alert('Please input a value');
  } 
  else if(textInput.value == 'A'){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "eye"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "_eye"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "race car"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "never odd or even"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "0_0 (: /-\ :) 0-0"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "My age is 0, 0 si ega ym."){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(textInput.value === "A man, a plan, a canal. Panama"){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else if(alphanumericPal.test(textInput)){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  else{
    resultElement.innerText = `${textInput.value} is not a palindrome`
  }
}






/* file: styles.css */
body{
  background-color: #030333;
  color: white;
  text-align: center;
  padding: 20px;
}
h1{
  font-size: 3rem;
}
.input-div{
  background-color: white;
  color: black;
  border-radius: 10px;
  height: 100px;
  margin: 30px;
   box-shadow: 0px 8px 5px #e940b6;
   padding: 15px;
}
#text-input{
margin: 30px;
border: none;
border-bottom : 5px solid purple
}
#check-btn{
  background-color: purple;
  color: white;
  border-radius: 5px;
  border: none;
  width: 90px;
  height: 30px;
  font-weight: bold
}
.text-div{
   background-color: green;
  color: white;
  border-radius: 10px;
  padding: 10px;
}
.text-div p{
  font-weight: bold;
  
}

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Please rethink your approach to this assignment and use JavaScript to determine if the text input is a palindrome rather than hardcoding expected results based on the user stories.

Also, how can just applying a regular expression to the text input determine if the text is a palindrome? What else do you think you would need to do to determine that? It might be a good idea to start by making comments in your code for each step to take.

For example, your first step could be:

// create a regular expression to remove spaces and any characters that are not alphanumeric

Your second step could be:
// test the regular expression at regex.com or by using console.log()

What do you think you would need to do next?

//create a statement that checks if the regular expression is a palindrome

Tell us what’s happening:

number 10 and 14 failed to pass, even when I checked my alpha on the console, it has the required text

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="eng">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> Palindrome Checker Project</title>
    <link rel="stylesheet" href="styles.css">
    
  </head>
  <body>
    <p>ChinazaEkpere</p>
    <h1>Is it a Palindrome?</h1>
    <div class="input-div">
      <label for="text-input" id="label">Enter in text to check for a palindrome:</label><br>
      <input id="text-input" >
      <button id="check-btn" >Check</button>
      <span id="result"></span>
    </div>

    <div class="text-div">
      <p>💡 A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.</p>
    </div>
    
    <script src="script.js"></script>
  </body>
</html>
/* file: script.js */
const checkBtn = document.getElementById("check-btn");
const textInput = document.getElementById('text-input');
 
const resultElement = document.getElementById('result');

checkBtn.addEventListener("click", () =>{

 const alpha =  textInput.value.replace(/[^A-Za-z0-9]/g, "");
  console.log(alpha)
  if(textInput.value === ''){
    alert('Please input a value');
  }
  else if(textInput.value.length ===1){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  
  else if(alpha === [...alpha].reverse().join('')){
    resultElement.innerText = `${textInput.value} is a palindrome`
  }
  
  else{
    resultElement.innerText = `${textInput.value} is not a palindrome`
  }
})


/* file: styles.css */
body{
  background-color: #030333;
  color: white;
  text-align: center;
  padding: 20px;
}
h1{
  font-size: 3rem;
}
.input-div{
  background-color: white;
  color: black;
  border-radius: 10px;
  height: 100px;
  margin: 20px;
   box-shadow: 0px 8px 5px #e940b6;
   padding: 15px;
}
#text-input{
margin: 30px;
border: none;
border-bottom : 5px solid purple
}
#check-btn{
  background-color: purple;
  color: white;
  border-radius: 5px;
  border: none;
  width: 90px;
  height: 30px;
  font-weight: bold
}
.text-div{
   background-color: green;
  color: white;
  border-radius: 10px;
  padding: 10px;
}
.text-div p{
  font-weight: bold;
  
}

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

Have you tried to print to console the reversed version as well? And/or result of the comparison between the two?

still don’t get it, please put me through

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.

you will need to try the debugging suggested, and show the result

just saying you are not getting the correct result doesn’t show you are trying

also, I have merged your two topics on this challenge, please do not open multiple topics for the same challenge

please teach me how to merge questions, to enable me not to open multiple topics for the same challenge.

I read about regular expression(regex) which I did in my recent question .
below is what console.log(alpha) got

A
eye
eye
racecar
notapalindrome
AmanaplanacanalPanama
neveroddoreven
nope
almostomla
Myageis00siegaym
1eyeforof1eye
0000
fivefour
07vqqv70
jmrh
``

you can’t

but if you have more questions on the same project and need to show the updated code you can do so in a new post in the existing topic

1 Like

And how look the reversed strings in these cases?

try to console log also the reverse:

do the two values match exactly?

when I try printing the reversed the console shows error uncaught …

but when I try a new code it gives me the reverse method

const str= "Good, morning!";
var reversedStr = str.split("").reverse().join("");
console.log(reversedStr)

the console prints
!gninrom ,dooG
!gninrom ,dooG

I am tried :disappointed_relieved: guess I need to start over again from the first javaScript algorithm

What error exactly?

Try the example from your last post with case that’s causing troubles - for example 'AmanaplanacanalPanama'

it is using the else statement

What will you get when you reverse 'AmanaplanacanalPanama'?

my code

let reverse = /[^A-Za-z0-9]/g
 console.log(reverse.reverse())

console output
Uncaught TypeError: reverse.reverse is not a function

That looks different than in your other post:

let reverse = /[^A-Za-z0-9]/g
 let reversedStr = reverse.split("").reverse().join("");
console.log(reversedStr)

console output
Uncaught TypeError: reverse.split is not a function

And where’s the string to reverse?