Code Review
Cleaning the String: Using regex and the replace function, non-characters are stripped for comparison
Loop: Using the clean version, the for loop starting at the end of the string creates a new string
Comparison: Comparing the cleaned version to the reversed string (plus the clean string length) determines if the string provided is a palindrome
// See The Code
function reverseString() {
// Get the user's word
let userWord = document.getElementById('tacoCat').value;
// Replace anything that is not a character or digit
const regex = /[^a-z0-9]/gi;
let cleanedWord = userWord.replace(regex, '').toLowerCase();
let reverseWord = '';
let start = cleanedWord.length - 1;
for (let i = start; i >= 0; i--) {
reverseWord += cleanedWord[i];
}
// Return results
let output = document.getElementById('result');
let msg = `${userWord} reversed (characters only) is: `;
output.innerText = msg + reverseWord;
// Is this a palindrome
let palOutput = document.getElementById('isPalindrome');
let isPalindrome = cleanedWord === reverseWord;
const winner = '<img src="/img/TACOCAT_WINNER.gif" alt="winner">';
// Must have 2 or more letters
if (isPalindrome && cleanedWord.length > 1) {
palOutput.innerHTML = `<h3>${userWord} is a palindrome.</h3>${winner}`;
} else {
palOutput.innerHTML = `<h3>${userWord} is NOT a palindrome.</h3>`;
}
}