Arrow Dynamics explainer

Aim of the puzzle: Use an arrow function
Walk through of solution: An arrow function is like a regular function, except with a different syntax. They are a useful way to quickly write a function using less code than one would with the function keyword. This is why they are often used for callback functions.

In this puzzle, the getAverage function takes a parameter called numbers. It declares a local variable called sum and sets its value to 0.

The for loop iterates through the array of numbers, and adds each number to sum. After the for loop finishes, the last line of the function returns sum / numbers.length.

To complete the puzzle, add sum += number to the code block of the for loop. Then, change the return statement to return sum / numbers.length.
Sample code solution:

let numberList = [15, 20, 13, 47];

let getAverage = (numbers) => {
    let sum = 0;
    for (let number of numbers) {
        sum += number;
    }
    return sum / numbers.length;
};

console.log(getAverage(numberList));

Javascript Concepts: Functions, Arrow Functions, Arrays, For Loops, Output Variables, console.log, Return Statements

1 Like

Why we should devide by numbers.length and not the actual array length (numbersList.length)?

1 Like

Hey there,

numbers is the name of the parameter of the function. When the function is called with numberList as an argument, the value of numbers is set to numberList, which means the function adds up each number in the array, and then returns the sum divided by the length of the array.

Hope this clarifies things. Let me know if you have any questions.
Ben

What are the values of ‘number’ and ‘numbers’? Please explain!