Array and Function


#1

I am trying to figure out how to solve this problem. I know how to do functions and array's, but I am unsure how to combine the two. Any help would be appreciated.


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.


#2

@datapro87479,

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#3

@datapro87479,

So you have to create a function with the function-name add
The function should take 1 parameter numbers

This numbers parameter is used as a variable throughout the FUNCTION-BODY

The function expects the numbers variable to be an Array,
As you are asked to create a total
you will have to declare a
var total = 0;
With numbers.length you will know how many Array-Elements you have....
You will have to use a FOR-loop to go over all those Array-Elements
and increment the total.

As the FOR-loop is ended you will have use
return total;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


#4

@datapro87479,

As you created the add function as having 1 parameter
you will have to call the add function providing 1 argument
in our case an Array
like
add([1,2,3,4]);

But as you used the return statement
you will get a Result with NO-Display
Therefor you want to use
console.log( add([1,2,3,4]) );