The Code
// get the user input to reverse
function getValues() {
// get the text out of the input and assign it to a variable
let message = document.getElementById('userInput').value;
// validate input and make sure its not empty
if (message.length == 0) {
// send a Swal message back if the input is not valid
Swal.fire(
{
backdrop: 'error',
backdrop: false,
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(message);
// give th results to display messare
displayResults(message, revMsg);
}
}
// reverse the string
function reverseMessage(input) {
// define return
let output = '';
// use a for loop to provide the variable with details
for (let i = input.length - 1; i >= 0; i--) {
output += input[i];
}
return output;
}
function displayResults(msgEntered, revMsg) {
document.getElementById('msg').textContent = `Your message is ${msgEntered}`;
document.getElementById('msg').textContent = `Your reversed message is ${revMsg}`;
}
First we ask the user to input a value.
display message
displays a sweetalert
when the button is pressed and if the user has not
entered a string to check