Skip to main content
added 38 characters in body
Source Link
OPTIMUS PRIME
  • 1.4k
  • 11
  • 22

I need to Sum all elements of array, up to the selected value of input (first four elements in this example):

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum); } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

I need to Sum all elements of array, up to the selected value of input:

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum); } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

I need to Sum all elements of array, up to the selected value of input (first four elements in this example):

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum); } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

Improved formatting
Source Link
Rui Jarimba
  • 18.5k
  • 11
  • 64
  • 101

I need to Sum all elements of array, up to the selected value of input:

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum);  } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

I need to Sum all elements of array, up to the selected value of input:

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum);  } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

I need to Sum all elements of array, up to the selected value of input:

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum); } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO

Source Link
OPTIMUS PRIME
  • 1.4k
  • 11
  • 22

Sum only required array elements

I need to Sum all elements of array, up to the selected value of input:

HTML:

<input id="VAL" value="4" /> <button onclick="SUM()">BUTTON</button> SUM: <span id="bubu"></span> 

JS:

var numbers = [1, 2, 3, 4, 5]; function getSum(x,y) {return x+y;} function SUM() { document.getElementById("bubu").innerHTML = numbers.reduce(getSum); } 

Now the sum of all elements work correctly. But how can I add a loop, to get only required sum?

Fiddle DEMO