The Code
// get the user's message to reverse
function getValues() {
// get the text input from the page
let message = document.getElementById('userInput').value;
// validate the input: make sure the input is not empty
if (message.length == 0) {
Swal.fire ({
icon: 'error',
backdrop: false,
title: 'Oops!',
text: 'Please enter a message to reverse'
});
} else {
// pass the input to reverseMessage() and assign its return value to a variable
let revMsg = reverseMessage(message);
// give the reverse message to displayessage()
displayMessage(revMsg);
}
}
// reverse the message
function reverseMessage(input) { // if input = hello
let output = ''; // output = ['h', 'e', 'l', 'l', 'o']
for (let i = input.length-1; i >= 0; i--) {
output += input[i]
}
// turns ['o','l','l','e','h'] into ['olleh']
return output;
}
// display the reverse message
function displayMessage(message) {
document.getElementById('msg').textContent = `Your message reversed is: ${message}`;
document.getElementById('alert').classList.remove('invisible');
}
Have you ever wondered how to reverse a message you've entered?
getValues() in this JavaScript code is used
to retrieve the user's input from a webpage, and also
make sure the input is not empty. I use a loop to go
through each character of the input message and build a
reversed message. For example, if the input is "hello,"
our code will turn it into "olleh." Pretty cool, right?
I use JavaScript to update an element on the webpage with the reversed message and make another element visible. This way, the user can see the result. And that's it!
You've just learned how to create a simple web page that takes a message, reverses it, and displays the result.