• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

240
Views
How do I get the calculation of the sum of the items in an array with a for loop?

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 + " ";
}
about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error