Hi, thanks for your reply. I think it will run when it is loaded as it is not attached to an event listener.
Should I instead have (organise === inputText.value.toLoweCase())? I am trying to compare the result of the checkPalindrome function to the input text value to check if it is the same and therefore a palindrome.
const inputText = document.getElementById("text-input");
const buttonCheck = document.getElementById("check-btn");
const checkResult = document.getElementById("result")
buttonCheck.addEventListener("click", () => {
if (inputText.value === "") {
alert("Please input a value")
return;
}
const checkPalindrome = (str) => {
const organise = str.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
return organise.split("").reverse().join("");
}
if (checkPalindrome(inputText.value)) {
checkResult.textContent = `${inputText.value} is a palindrome`;
} else {
checkResult.textContent = `${inputText.value} is not a palindrome`;
}
});
I have now placed everything in the event listener and I am getting a result but everything I enter says ’ x is a plaindrome’. For example ‘map is a palindrome’.
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.