So I am a bit lost on this one, so what I did was create the for loop that shows their index, for example: 0 1 2 3 4 5 6 7 8 9.
let tasks =["swim", "study", "eat", "break", "sleep", "Cook", "Clean", "watch TV", "Train", "Code"]
let x;
for(x of tasks)
{
document.getElementById("answers").innerHTML += x + " ";
console.log(x);
}
so the above code I used the for of loop which gets index of each item. What I am trying to do is how do I get the add each index to each other, so it should be 1 + 2 + 3...etc.
//Activity 2 - Edit the loop created above, to now calculate the sum of all the items leading up to 10. e.g. 1 + 2 + 3 = 6
//console.log your output
//Add your code below
let index;
let sum;
for(x = 0; x < tasks.length; x++)
{
index = document.getElementById("sumOftasks").innerHTML += x + " ";
}
Your code will return values of array and when you sum it it Will give you result NaN
You can get value from array, Find index and Sum it.
for(x of tasks)
{
let index = tasks.indexOf(x);
document.getElementById("answers").innerHTML += index + " ";
}
It will help.