Return string as an array?

Very unsure as to why my answer is returning in a string and not as an array. Is it because of concatenation?

The sum function should add up an array of numbers into a single number. For example:

sum([]) // 0
sum([ 1 ]) // 1
sum([ 1, 2 ]) // 3
sum([ 1, 2, 3 ]) // 6

function sum (numbers) {
  let arr = [];
  for(let i=0; i < arr.length; i++){
      arr += numbers[i];
  }
  return arr;
}

If you are trying to add elements to an array, you use .push()

Yeah, I tried push and just got messed up with the answer. Still tinkering…

here’s your issue
arr is an array not a number
if you want to use += to add numbers you need a number on both sides (left and right)

can you think of how to user arr to get a single numer?

Tried this but still nothing:

function sum (numbers) {
  let arr = [];
  for(let i=0; i < arr.length; i++){
      arr.push(numbers[i]);
  }
  return arr;
}

Bah, I’m a dunce and once again overthinking…thanks

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

Will definitely get to it and post whatever result I can here!

Tried this and now a bit stuck.

function sum (numbers) {
    let sum = 0;
    return sum += sum.reduce(numbers);
  }

It has to have a function go through it? That’s what is confusing on this. And why two parameters??

function sum (numbers) {
    return numbers.reduce(function(acc, numbers){
    return acc + numbers; 
    });
  }

Thanks, Randell!

Reducer works like a mixer.

You have 4 fruits: apple, banana, orange, strawberry.
Now you put the first one into the mixer => apple, 3 fruits left.
Now you put the second one into the mixer => apple + banana, together applebanana, 2 fruits left.
Now you put the third one into the mixer => applebanana + orange, together applebananaorange, 1 fruit left.
This goes until all fruits are gone and we have applebananaorangestrawberry.

In my example, the reduce function would be (a,b) => a + b, because I add the fruits together.
a is the accumulation of the fruits, e.g. applebanana,
b is the next value, e.g. orange.

The next time,
a would be. applebananaorange,
b the next value, strawberry.

This?

function sum (numbers) {
    return numbers.reduce(function(acc, num){
    return acc + num; 
    });
  }