Tell us what’s happening:
Hello, I cannot remotely figure out what is causing my palindrome checker function to always evaluate to true. I am assuming it has something to do with my array reversal but I can’t pinpoint the issue and my code is not showing any errors.
Please let me know where I went wrong.
Your code so far
/* file: script.js */
const checkButton = document.getElementById('check-btn');
const userInput = document.getElementById('text-input');
const displayResult = document.getElementById('result');
//main function declaration
const checkForPalindrome = (input) => {
const regex = /[a-z0-9]/gi;
const originalInput = input;
//string manipulations
const lowercaseString = input.toLowerCase().replace(regex, '');
const reverseString = lowercaseString.split('').reverse().join('');
//alert for no input
if (input === ''){
alert (`Please input a value`);
return;}
//checking for palindrome and write HTML message
let resultsMessage = `${originalInput} ${lowercaseString === reverseString ? 'is' : 'is not'} a palindrome`;
//returning results to html
//new p tag
const pTag = document.createElement('p');
pTag.className = 'user-input';
pTag.innerHTML = resultsMessage;
displayResult.appendChild(pTag);
};
//button eventlistener
checkButton.addEventListener("click", () => {
checkForPalindrome(userInput.value);
userInput.value = '';
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker