S
S
Sh00tNik2017-01-09 01:09:32
JavaScript
Sh00tNik, 2017-01-09 01:09:32

Stack elements in JavaScript array?

Here is the condition.

var list = [1, 2, 3, 4, 5, 6];

function sum() {
var sum = 0;
 for (i=0; i<arguments.length; i++) {
  sum += arguments[i];
 }
}

Подсчитать с помощью функции
sum
сумму элементов
list

The task is here. No. 10
www.pvsm.ru/javascript/37681
I don't understand, was it implied here that it was possible to edit the function code, or should it be called somehow cunningly? In addition, it does not have a return. How to deal with her?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leo Developer, 2017-01-09
@Sh00tNik

It is necessary to calculate in a cunning way, but at the expense of editing, most likely this is a mistake of the author of this site

var list = [1, 2, 3, 4, 5, 6];

function sum() {
  var sum = 0;
  for (i=0; i<arguments.length; i++) {
    sum += arguments[i];
  }
  return sum
}

sum.apply(null, list) // output => 21

A
akzhan, 2017-01-09
@akzhan

You are right, add return sum there, and at the end console.log(sum(list));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question