//controller function
function getValue() {
let inputWordRaw = document.getElementById("wordHTML").value.toLowerCase();
let inputWord = inputWordRaw.replace(/[^a-zA-Z0-9]/gi, "");
let palCheck = palindromeCheck(inputWord);
displayCheck(palCheck);
}
function palindromeCheck(message) {
//string = array of characters
let reversedMessage = "";
for (let i = message.length - 1; i >= 0; i--) {
reversedMessage += message[i];
}
if (reversedMessage == message) {
Swal.fire({
backdrop: false,
title: "Yep,",
text: "That's a palindrome!",
});
} else {
Swal.fire({
backdrop: false,
title: "Nope",
text: "That is not a palindrome",
});
}
}
The code is structured in two functions:
getValue()
Collects a string that the user has input.
palindromeCheck()
uses a "for" loop to reverse the message, then checks if the reversed message matches the original message. It then displays the answer with a Sweet Alert.