The Code for Rewind
// Get the string from the page
function getValue() {
document.getElementById("alert").classList.add("invisible");
let userString = document.getElementById("userString").value;
let revString = resverseString(userString);
displayString(revString)
}
// Reverse the String object
function reverseString(userString){
let revString = [];
// reverse string
for (let i = userString.length - 1; i >= 0; i--) {
revString += userString[i];
}
return revString;
}
// Display results
function displayString(reverseString){
// Write message to page
document.getElementById("message").innerHTML = `Your string reversed is: ${reverseString}`
// Show alert box
document.getElementById("alert").classList.remove("invisible");
}
The Code is structered into three functions.
getValue
getValue accepts(gets) the user input from the page. It utilizes getElementById to pull the vallues from the page. It passes those values to the reverseString function. The fuction reverseString returns the reversed string and passes that to the displayString function.
reverseString
reverseString takes in one parameter, userString. We create a variable (revString) that holds an array. Then a For loop is used to iterate through userString backwards then return the results.
displayString
displayString takes in one parameter, the reversed string. It then removes the class invisible and displays the alert box housing the reversed string.