Tell us what’s happening:
Hi, i’m having trouble with the 15th step of the Palindrome Checker project. Every other test seems to run smoothly but the “1 eye for or 1 eye” feels impossible… I believe the problem is within the regex but at this point i’m not even sure… can you take a look please? Thank you.
EDIT: If i log the phrase happens this
console.log(cleanInput("1 eye for of 1 eye"))
// 1eyeforof1eye
// result - is a palidrome
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
href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Lato&family=Roboto+Mono&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="styles.css" />
<title>
Palindrome Checker
</title>
</head>
<body>
<div class="container">
<p>Hi, care to see if your word is a palindrome?</p>
<input id="text-input"></input>
<button id="check-btn">Check</button>
<p id="result"></p>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
let userInput = document.getElementById("text-input");
let checkBtn = document.getElementById("check-btn");
let result = document.getElementById("result");
checkBtn.addEventListener("click", isPalindrome);
function isPalindrome() {
if (userInput.value == "") alert("Please input a value");
let cleanStr = cleanInput(userInput.value);
for (let i = 0; i <= Math.floor(cleanStr.length / 2); i++) {
if (cleanStr[i] !== cleanStr[cleanStr.length - 1 - i]) {
result.textContent = `${userInput.value} is not a palindrome`;
} else {
result.textContent = `${userInput.value} is a palindrome`;
}
}
}
function cleanInput(str) {
const regex = /[\W_]/g;
return str.replace(regex, '');
}
/* file: styles.css */
.container {
display:flex;
flex-direction:column;
margin: 20px;
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker