I am having an issue getting this students function to work. I am pretty new to this subject, and have tried a couple of different approaches above, but still not getting the response we want. I am sure it is something silly I am missing. Can someone take a look?
When passing parameters to a function, you separate two different values with a comma:
function addNumbers(num1, num2){
return num1 + num2;
}
console.log(addNumbers(1, 5));
//Returns 6
An array has to be surrounded by brackets […], and each item is separated by a comma:
var numberArray = [1, 2, 3, 4, 5];
To pass the array as a parameter to a function, you keep the brackets in and pass it as you would a number, or a string:
function addNumbers(numberArray, str){
var addedNumber = 0;
for(var i = 0; i < numberArray.length; i++){
addedNumber += numberArray[i];
}
console.log(str);
return addedNumber;
}
console.log(addNumbers([1, 2, 3, 4, 5], "This string is the second parameter and will show in the console."));
//Displays the string parameter in the console
//then returns 15
In short - you’re just missing some brackets, I apologize if I made this a bit too long!