Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

I can’t get past the 5th test.

Your code so far

const btn=document.getElementById('check-btn')
const input=document.getElementById('text-input')
const result=document.getElementById('result')
const string= [input.value]

const cleanString=(string)=>{
  const regex=/[^\w\s]|_/g
 return string.toLowerCase().replace(regex,'')
}
const reversed=(string)=>{
 return cleanString(string).split('').reverse().join('')
}

function clickButton() {
if (input.value === "") {
  alert("Please input a value")
  
} 
 if (input.value.toLowerCase() === reversed(input.value)){
  result.textContent=`${input.value} is a palidrome`

 }else{
  result.textContent=`${input.value} is not a palidrome`
 }
  


  
}
btn.addEventListener("click",clickButton)

```html
<!-- file: index.html -->
<input id="text-input">
<button id="check-btn">Check</button>
<div id="result"></div>

<script src="script.js"></script>
/* file: styles.css */

/* file: script.js */

const btn=document.getElementById('check-btn')
const input=document.getElementById('text-input')
const result=document.getElementById('result')
const string= [input.value]

const cleanString=(string)=>{
  const regex=/[^\w\s]|_/g
 return string.toLowerCase().replace(regex,'')
}
const reversed=(string)=>{
 return cleanString(string).split('').reverse().join('')
}

function clickButton() {
if (input.value === "") {
  alert("Please input a value")
  
} 
 if (input.value.toLowerCase() === reversed(input.value)){
  result.textContent=`${input.value} is a palidrome`

 }else{
  result.textContent=`${input.value} is not a palidrome`
 }
  


  
}
btn.addEventListener("click",clickButton)

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

There’s a little typo in what is in the #result, it’s palindrome:

  result.textContent=`${input.value} is a palidrome`

 }else{
  result.textContent=`${input.value} is not a palidrome`
1 Like

What about the regression of _eye?Cant get past it.I have also placed it among punctuations that have to be empty.

‘’’
const btn=document.getElementById(‘check-btn’)
const input=document.getElementById(‘text-input’)
const result=document.getElementById(‘result’)
const string= [input.value]

const cleanString=(string)=>{
const regex=/[^\w\s]|_ /g
return string.toLowerCase().replace(regex,‘’)
}
const reversed=(string)=>{
return cleanString(string).split(‘’).reverse().join(‘’)
}

function clickButton() {
if (input.value === “”) {
alert(“Please input a value”)

}
if (input.value.toLowerCase() === reversed(input.value)){
result.textContent=${input.value} is a palindrome

}else{
result.textContent=${input.value} is not a palindrome
}

}
btn.addEventListener(“click”,clickButton)‘’’

Your logic is right but you missed little things.

To check if a word is palindrome, you ignore the spaces which you don’t remove in your regex you need to remove the \s from your character set in order to find those spaces and remove them.

const regex=/[^\w\s]|_/g

Then, in case of clicking the button without entering any value, you checked for this case and used alert but after that you need to stop executing the function and exit. So you need to add return after your alert.

if (input.value === "") {
alert("Please input a value")

}

Lastly…

if (input.value.toLowerCase() === reversed(input.value))

Here, you are checking the original input.value which may contain spaces or punctuation which you need to clean your input before comparing it to the reversed one. Then use your cleanString function on input.value.

I managed to correct the small errors. Now a new error came in regex.

 const regex=/[^\W_]/g

When I click “not a palindrome” it seems to take it as a palindrome.Which point am I missing?

const regex = /[^\W_]/g

This will match any alpha-numeric characters including underscore _, but in your code you want to replace any character that is NOT alpha-numeric including underscore with an empty string "".

So you have to make the regex match underscores and any character that is not alpha-numeric.

You can view your original posted regex on the topic and see why i told you to remove only the \s and keep the rest, and compare it with the one here.

But your new one here can be used with one little modification, remove the negated character ^ from your character set and it will work. Cause that way it will match any non alpha-numeric character \W and also the underscore _ which you will then replace the match result with empty string in your code "".

1 Like

Yes.It did work.Thanks alot!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.