Given the following code var myArray 123456 function someFu
Given the following code:
var myArray = [1,2,3,4,5,6];
function someFunc(array) {
var s = 0;
for(var i = 0; i < array.length; i++) {
s = s + array[i];
}
return (s / array.length);
}
var a = someFunc(myArray);
What will variable a\'s value be after the code finished? What is the purpose of the function someFunc?
A. Value: 4
The factorial value of all myArray\'s numbers
B. Value: 3.5
The average value of all numbers in myArray
C. Value: NaN
It doesn\'t do anything
D. Value: 21
Is the sum of all values
E. Value: 3.5
Some random number
Solution
var myArray = [1,2,3,4,5,6]; //Declares an array.
function someFunc(array) { //Function takes array as input.
var s = 0; //Initializes s to 0.
for(var i = 0; i < array.length; i++) { //Loop runs for values of i from 0 to 5.
s = s + array[i]; //Every value is added to the array.
}
return (s / array.length); //The average of the array elements is returned.
}
var a = someFunc(myArray); //The average of the array myArray is stored in a.
So, the average is: (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5
So, the answer is:
B. Value: 3.5
The average value of all numbers in myArray
![Given the following code: var myArray = [1,2,3,4,5,6]; function someFunc(array) { var s = 0; for(var i = 0; i < array.length; i++) { s = s + array[i]; } retu Given the following code: var myArray = [1,2,3,4,5,6]; function someFunc(array) { var s = 0; for(var i = 0; i < array.length; i++) { s = s + array[i]; } retu](/WebImages/22/given-the-following-code-var-myarray-123456-function-somefu-1052641-1761548618-0.webp)