Tell us what’s happening:
The “When the #text-input element only contains the letter A and the #check-btn element is clicked, the #result element should contain the text ‘A is a palindrome’.” Check and all similar checks aren’t reading my result element’s text even though it’s clearly there on the preview screen.
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">
<title>Document</title>
</head>
<body>
<form id="palindrome search">
<input required
type="text"
id="text-input" placeholder="eg. 'TACOCAT'"></input>
<button type="submit" id="check-btn">submit</button>
</form>
<span id="result"></span>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
const searchBar = document.getElementById("text-input");
const checkBtn = document.getElementById("check-btn");
const resultText = document.getElementById("result");
function getInput(){
if(searchBar.value.length > 0){
let inputStr = searchBar.value;
if(isPalindrome(inputStr.replace(/[\W_]/g, '').toLowerCase())){
resultText.textContent = `"${inputStr} is a palindrome"`
console.log(resultText.innerText);
}
else{
resultText.innerText = `"${inputStr} is not a palindrome"`
console.log(resultText.innerText);
}
}
else{
//searchBar.setCustomValidity("Please input a value");
alert("Please input a value");
}
}
function isPalindrome(str){
let count = 0;
for(let i = str.length-1; i > Math.floor((str.length-1)/2); i--){
if(str[i] == str[count]){
//console.log(str[i]);
count++;
}
else{
return false;
}
}
return true;
}
checkBtn.addEventListener("click", getInput);
/* file: styles.css */
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