Adding up Array's

Adding up Array's
0.0 0

#1

Hey, I have these instructions:

Create a function called addThemUp that takes in an array of numbers and returns the total of all of the items in the array added together.

Here’s my code, any help is appreciated!

var addThemUp[1,2,3,4,5];

addThemUp.reduce(function(a,b){
  return a+b;
};

#3

Not sure I understand what it is you mean…


#4

You need to fix that first line :

var addThemUp[1,2,3,4,5];

to

var addThemUp = [1,2,3,4,5];


#6

Thanks that makes a lot of sense! I need the function to be addThemUp so I switched them, however I’m getting this error now:

Function returned 
15
instead of 
6
when passed 
[1,2,3]

#7

Can you show us your current code ?


#8
function addThemUp() { 
	var runMe = [1,2,3,4,5];

	return runMe.reduce(function(a,b){
  	return a+b;
	}); 
} 

console.log(addThemUp());

#9

This code looks good to me, you are getting 15 as a result. and if you change addThemUp to

var addThemUp = [1,2,3];

you’ll get 6. Was this the needed result ?


#10

Unfortunately not, I tried that and it just reverses the error :confused:

Function returned 
6
instead of 
15
when passed 
[1,2,3,4,5]

#12

Okay, this is my code now:

function addThemUp() { 
	var runMe = [1,2,3,4,5];

	function addThemUp(runMe) {
  	return runMe;
	}; 
} 

console.log(addThemUp());

I’m being told it returns as Undefined


#14

Sorry, I was playing around with the coding and must’ve changed it, however I do see what the issue was now.

Cannot read property 'reduce' of undefined


#15

You function argument needs to be an array in order for .reduce to work. You need to pass this argument in the function call :

addThemUp([1,2,3]);


#17

That last edit was it! Thank you so much for taking the time =)