Function basics

What you need to know for tomorrow's assessment.

There are three steps, each of which will earn you some credit.

Step 1

Write a correct skeleton for the function.

const averageWeight = () => {
  return ;
};

Key elements are the correct function name, all the punctuation (though the semicolons are optional), and the return.

If you do nothing else, write this much for each function to get some credit for knowing the correct syntax for a function definition.

Step 2

Add the right number of arguments to the argument list, giving them meaningful names.

const averageWeight = (totalWeight, itemCount) => {
  return ;
};

You should be able to read off the number of arguments required from the function description.

Choose names that reflect how the arguments are described but remember that arguments names must start with a letter and contain only letters and numbers.

Step 3

Add an expression after the return that produces the value called for in the function decription.

const averageWeight = (totalWeight, itemCount) => {
  return totalWeight / itemCount;
};

Often the whole function body will just be a return and an expression.

You can also introduce variables if that makes the code simpler or use control constructs like if statements or loops if appropriate. (Not likely on this assessment.)

Step 4

If possible, simplify the code to make it easier to tell just by reading the code what it does.

Ask yourself, could someone seeing just the code have a chance of writing something like the English description of the function that you started from?

Next

Numeric review