Hi, I am trying to complete the Palindrome Checker Project.
I wrote my code on VS code, all looks perfect to me. I did all the tests manually and they all work, but freecodecamp still thinks my tests are running wrong.
Any feedback is really appreciated thank you
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palindrone Checker</title>
</head>
<body>
<h1>Palindrome Checker</h1>
<input type = "text" id = "text-input">
<button id ="check-btn" onclick = "press()">Press me</button>
<div id ="result">Results Div</div>
<script src="script.js"></script>
</body>
</html>
type or paste code here
javascript:
const textInput = document.querySelector("input");
let filterInput = textInput.value;
let textLength = filterInput.length;
let resultEl = document.getElementById("result")
textInput.addEventListener("keyup", () => {
filterInput = textInput.value.replace(/[^A-Z0-9,. ]/ig, "");
textLength = filterInput.length
filterInput = filterInput.toLowerCase();
});
function press() {
if (textLength === 0) {
alert ("Please input a value")
} else {
check()
}
}
function reverseString(filterInput) {
var splitString = filterInput.split("");
var reverseArray = splitString.reverse();
var joinArray = reverseArray.join("");
return joinArray
}
function check() {
let reverseEl = (reverseString(filterInput))
console.log(filterInput)
console.log(reverseEl)
if (filterInput === reverseEl) {
console.log("Palindrome")
resultEl.textContent = (textInput.value + " is a palindrome");
} else {
console.log("Not a Palindrome")
resultEl.textContent = (textInput.value + " is not a palidrome");
}
}
Thank you so much for your reply, so I tried putting everything into the “press” function.
All tests are working now, except for when its not a palindrome. It still says its not passing.
Really not sure why
edit: I think its because of the else if, and then the final else condition, if I make a “check function” that does the whole check it wont pass the tests if its a palindrome or not
let resultEl = document.getElementById("result")
function press() {
let textInput = document.querySelector("input");
let filterInput = textInput.value.replace(/[\\,_.0-9 ]/ig, "");
filterInput = filterInput.toLowerCase()
let textLength = filterInput.length;
console.log(textLength)
let reverseEl = (reverseString(filterInput))
console.log(filterInput)
console.log(reverseEl)
if (textLength === 0) {
alert ("Please input a value")
} else if (filterInput === reverseEl) {
resultEl.textContent = (textInput.value + " is a palindrome");
console.log("Palindrome")
} else {
resultEl.textContent = (textInput.value + " is not a palidrome");
console.log("Not a palindrome")
}
}
function reverseString(filterInput) {
var splitString = filterInput.split("");
var reverseArray = splitString.reverse();
var joinArray = reverseArray.join("");
return joinArray
}