Exercise 4 (Parameters II)

Codecademy lists this as their example of setting two parameters:

const getAverage = (numberOne, numberTwo) => {
const average = (numberOne + numberTwo) / 2 ;

getAverage(365, 27);
//output 196

However, unless I’m mistaken, you can get the same result by doing this:

const getAverage = (numberOne, numberTwo) => {
console.log((numberOne + numberTwo) / 2 );

getAverage(365, 27);
//output 196

What is the benefit of declaring the “average” variable within the getAverage function, compared to writing the function without it? Thanks!

It’s true that you can get the same output the second way. The lessons are just trying to help you develop a habit of doing calculations independently of the actual console lines. In this specific case it’s not a terribly big deal, but it’s a good habit to develop where you’re just using console logging to show the results of operations, as opposed to performing the operations themselves.

1 Like

Oh ok got it. Thanks!

If we make the function the abstract operation, we can log the result when it is returned. A function with the word get in the name implies something to be got and returned, not logged by the function itself. Returning a value sets wheels in motion, whereas logging is a one-off operation with no lasting result.

const getAverageOfTwoValues = (a, b) => (a + b) / 2;

console.log(getAverageOfTwoValues(365, 27));    // 196

Part of coding practice is documentation. Simple code can be self-documented by the names we use on the functions and the running variables. Above, we use a and b to represent two unknown values, which is adequate given that the name of the function says it all.

One school of thought is to put everything in variables which is essentially a label that says what it represents.

const average = (a + b) / 2;
return average;

The degree of verbosity will depend upon the stage of development. When sketching out ideas verbosity is helpful but once the testing is completed we need to trim the fat and simplify the code so it is both lightweight and easily readable.

To be clear, variable and function names are meant for human readers, not the computer. They help to describe the intermediate steps and expected outcomes at each stage. If code is readable and discernible in simplified form, then that is what to write.

1 Like

Hey, sorry, just saw this now. But this is helpful, thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.