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?