The Code
function getValues() {
// get the text out of the input and assign it to a variable
let message = document.getElementById('userInput').value;
// clean up the values as needed by using regex
let regex = /[^a-zA-Z0-9]/g;
let cleanInput = message.replaceAll(regex, '');
// validate input and make sure its not empty
if (cleanInput.length == 0) {
Swal.fire(
{
backdrop: 'error',
backdrop: false,
//replace this with that variable
text: 'Please enter a string to check'
}
);
} else {
// pass the user input to reverseMessage function and
// assign it's return to a variable
let revMsg = reverseMessage(cleanInput);
//send the message adn reverse message to checkForPalindrome
let palindrome = checkForPalindrome(cleanInput, revMsg);
// give th results to display messare
displayResults(cleanInput, revMsg, palindrome);
}
}
// reverse the string
function reverseMessage(input) {
// define return
let output = '';
for (let i = input.length - 1; i >= 0; i--) {
output += input[i];
}
return output;
}
// check to see if the strings match
function checkForPalindrome(msg, revmsg) {
if (msg == revmsg) {
return true;
} else {
return false;
}
}
function displayResults(msgEntered, revMsg, PalTrueFalse) {
if (PalTrueFalse == true) {
document.getElementById('msg').textContent =
`Your message of ${msgEntered} is a palindrome!`;
} else {
document.getElementById('msg').textContent =
'Sorry, but your message is not a palindrome';
}
}