I now this code works if I have preset values defined in my array, how would I go about doing this when the values are not defined? I have a hard time with all loops in general especially for loop.
I know there are methods to achieve this, but for the purpose of my assignment I must achieve it using a for loop.
function sum() {
let input = document.getElementById("wordInput").value;
let values = [];
let sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i]
if (values) {
document.getElementById("resultReturned").innerHTML = " Total is" + sum;
}
}
}
Give your function a parameter. This way you can pass any array to your function and loop over it, no matter the length.