Linked Questions

297 votes
35 answers
740k views

How to compute the sum and average of elements in an array? [duplicate]

I am having problems adding all the elements of an array as well as averaging them out. How would I do this and implement it with the code I currently have? The elements are supposed to be defined as ...
jonathan miller's user avatar
51 votes
9 answers
164k views

Sum of values in an array using jQuery [duplicate]

I have an array containing some values and I want to get their sum. Here is the example: var somearray = ["20","40","80","400"]; I want to sum these values using jQuery. In this example the result ...
Aman Virk's user avatar
  • 3,967
4 votes
4 answers
45k views

Calculate sum with forEach function? [duplicate]

I want to calculate sum with forEach array function in JavaScript. But I don't get what I want. function sum(...args) { args.forEach(arg => { var total = 0; total += arg; console....
Milos N.'s user avatar
  • 4,887
1 vote
1 answer
29k views

With Javascript use a for loop to sum numbers in an array [duplicate]

I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work: var total = 0; for (var i = 0; i < totalPrice.length; i++); total += ...
Sergi's user avatar
  • 1,250
1 vote
7 answers
8k views

How to multiply all items in array inside a loop with JavaScript [duplicate]

I have dynamic input of items 0 to N in a function (JavaScript). In each position have value like items[1,3,4], creating a loop: update: items or "the array" is dynamic I don't have permanent values....
user1581986's user avatar
1 vote
4 answers
9k views

Sum arrays in array (JavaScript) [duplicate]

I have an array that consists of multiple arrays: var array = [[1], [2, 1, 1], [3, 4]]; Now I want to get an array that has elements that are the sums of each array in the variable "array". In this ...
Lukas Hochholzer's user avatar
1 vote
5 answers
3k views

Sum negative and positive numbers in array to get a result [duplicate]

i'm stucked with a simple logic, i have an array as a result that contains positive and negative numbers. what i need is to sum both negative and positive and print a result const arr = ['-8', '-3', '...
MarkLearningCode's user avatar
0 votes
5 answers
5k views

Make an array filled with numbers 1 to 10000 and then sum all of those numbers [duplicate]

How can I turn this into a function that takes an array of any length and gives you the total? var points = new Array(100); for (var i = 0; i < 100; i++) { points[i] = i + 1; } for(var i = 0;...
Michael Melin's user avatar
2 votes
5 answers
1k views

There is a blank array and i need add numbers [duplicate]

I have a blank array and i need to add the numbers from 1 to 20 to this array. After that i need to sum these number totally. I am stucked in here: for(i=1;i<=20;i++){ push() } What do you ...
dr.bill.al's user avatar
0 votes
1 answer
2k views

Using map method to find a total? [duplicate]

So let's say I have some data. const cart = [ [{name: "Jose", total: 5}], [{name: "Rich", total: 10}] ] How do I total up those totals using map or forEach?
Dres's user avatar
  • 1,509
0 votes
1 answer
368 views

Sum an array of milliseconds [duplicate]

I'm having an array of milliseconds which I want a total of. I have tried the array.reduce and use a for-loop but all I get is total gibberish when I console it or use on the site. If I get the ...
Thomas Bengtsson's user avatar
0 votes
1 answer
315 views

Calculate sum of values in Object of arrays [duplicate]

I have a json in the below format let newarrayObj = [ { "cost one": "118", "cost two": "118", "cost three": "118" }, ...
Deepak Murthy's user avatar
0 votes
1 answer
257 views

Calculating the sum of a var [duplicate]

I am trying to calculate the sum of my 'var time' so that it can display the total on my table. Could someone tell me how do I go about doing this? <td> <h3 id="Luke Hours"> ...
Richard Siaw's user avatar
0 votes
7 answers
171 views

How to add up values stored in an array created using push prompt [duplicate]

I'm prompting the user to enter how many numbers they would like to store in an array. Then I prompt user to enter each individual number and push this into an array, I can display the numbers ...
Daniel R's user avatar
0 votes
1 answer
262 views

Recursive counting and find the sum of the given values [duplicate]

hoping someone can point me in the right direction here. I have to add the pages of each book and get a total at the end. I was able to do a function that pulls the values from the pages attribute, ...
Strife_x7x's user avatar

15 30 50 per page
1
2 3 4 5
7