The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.
Loops and arrays
for
loops are handy for traversing arrays.
In the following example, the elements of an array are added together:
let sum = 0;
for (let i = 0; i < myArray.length; i++) {
sum = sum + myArray[i];
}
Exercise
Write a function
Example:
mean
that accepts an array filled with numbers
and returns the arithmetic mean of those numbers.Example:
mean([1, 2, 3])
should return (1+2+3)/3
= 2
.
+ Hint
To calculate the mean of n numbers, you have to add up the numbers and divide the obtained sum by n.
+ Solution
function mean(data) {
let sum = 0;
for (let i = 0; i < data.length; i ++) {
sum = sum + data[i];
}
return sum / data.length;
};