The Code
// entry point AKA Controller
function getValues() {
let userString = document.getElementById('userString').value;
let reversedString = reverseAString(userString);
displayString(reversedString);
}
// logic function
// reverse a string
function reverseAString(userString) {
let reversedString = ''
for (let i = (userString.length - 1); i >= 0; i--) {
reversedString += userString[i];
}
return reversedString;
}
// view function
function displayString(reversedString) {
document.getElementById('results').innerHTML = reversedString;
document.getElementById('alert').classList.remove('invisible')
}
The code is structured in three functions
getValues()
This function grabs the string input by the user.
reverseAString(userString)
This function will iterate through each item in the string reading back to front, and store each letter in a new, reversed array.
displayString(reversedString)
This function displays the reversed string in an alert box.