Return string as an array?

Return string as an array?
0

#1

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;
}

#2

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


#3

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


#4

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?


#5

Tried this but still nothing:

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

#6

The above code just returns an empty array, because for some reason you are basing the iteration of the for loop on arr’s length instead of numbers (the array with the actual numbers in it) length.

If you are wanting to return a number , then do not return an array. Have a variable (let’s call it sum) start at a specific value (I will let you figure that out) and then you can use the += numbers[i] on it and return sum.


#7

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;
  }

#8

There you go. You got it straight. Now, see if you can create the same function but use the reduce method instead of a for loop to return the sum of the values in the numbers array.


#9

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


#10

Tried this and now a bit stuck.

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

#11

Read through the reduce documentation and make sure you understand what the various parameters mean.

With reduce, your function will be something like:

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

OR if you arrow function syntax:

function sum (numbers) {
  return numbers.reduce( (???,???) => ??????????? );
}

NOTE: The long ????????????? above does not mean it is a single word/command.


#12

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


#13

It is a callback function which will execute on each iteration over the array. The first argument is the accumulator and the second argument represents the current element value being iterated over. Take a look at the reduce documentation examples.


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

Thanks, Randell!


#15

Good job! The only thing I would change, just to keep things clear for someone reading your code, would be to rename the numbers argument inside the callback to something other the numbers (which is the value passed into the original function). It does not affect your code that they are the same, because the numbers in the callback function is local to the callback function. Maybe call it num? Also, indent the return statement inside the callback function to make it more readable.


#16

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.


#17

This?

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

#18

As far as indentation, I was thinking:

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

Using arrow function syntax, you could write your entire function like:

const sum = numbers => numbers.reduce((sum, num) => sum + num, 0); 

You will notice I renamed acc to sum which better describes the value the accumulator contains. Also, you will notice I added a initial value (the 0) for the accumulator, so that if an empty array is passed into the function, it will return 0 for the sum.