Apps Script Course: Functions (continued)

There are also arrow functions, which have a shorter syntax than those seen previously.


Arrow Function

Let's revisit our "average" function from the previous page:

function average(number1, number2) {
  return (number1 + number2) / 2;
}

This same function can be written as an arrow function like this:

const average = (number1, number2) => (number1 + number2) / 2;

With the arguments between () to the left of the arrow (parentheses are not necessary if there is only one argument):

(number1, number2)

And the value to return to the right of the arrow:

(number1 + number2) / 2;

If you have more instructions than just the returned value, add {} and specify the value to return with return:

const average = (number1, number2) => {
  console.log('example');
  return (number1 + number2) / 2
};
When creating functions, you don't need to write them as arrow functions, the classic syntax works fine. Arrow functions are more often used as arguments in certain methods such as map, filter, sort, forEach, etc., which we will see later.

Exercise

In the last example, the "average" function returned the average of 2 numbers, which makes it unusable for calculating the average of a larger number of values.

The new "average" function should be able to return the average by considering all the numbers in the array passed as an argument (sum of all values divided by the number of values):

// Average based on an array of values
function average(numArray) {

  // To be completed...

}

Complete the "average" function before moving to the solution a little further down...

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

A commented solution:

// Average based on an array of values
function average(valueArray) {

  // Constant containing the number of values in the array
  const numValues = valueArray.length;

  // Variable that will contain the total of the values
  let total = 0;

  // Loop based on the number of values in the array
  for (let i = 0; i < numValues; i++) {

    // Add the value to the total
    total += valueArray[i];
  }

  // Returns the average, i.e., the sum of the values divided by the number of values
  return total / numValues;
}

This solution works very well provided that the array passed as an argument is always correct, which will not necessarily always be the case and will depend in particular on the origin of the data...

To start, you can check if there is an array and if the array contains at least one value.

We have here the constant numValues:

const numValues = valueArray.length;

If the array is empty, numValues will be 0 and if there is no array, numValues will be undefined.

So, you can add a condition to return an error message if numValues contains one of these values:

if (numValues === 0 || numValues === undefined) {
  return 'ERROR';
}

In this case, you can also simplify the condition by using the ! operator:

if (!numValues) {
  return 'ERROR';
}

It's still necessary to manage the case of text format values...

The desired behavior here is as follows:

One solution is to convert the value to a number and then check if it is indeed a number with the isNaN (is Not a Number) function:

// Conversion to number
const number = Number(valueArray[i]);

// If non-numeric
if (isNaN(number)) {
  return 'NON NUMERIC';
}

The "average" function is now capable of handling many special cases:

// To test with different data
function test() {
  console.log(average([10, 20])); // Displays: 15
  console.log(average([-40, 0, 10])); // Displays: -10
  console.log(average(['-40', '0', 10])); // Displays: -10
  console.log(average([0, 1, 2, 3, 4, 5, 6, 7])); // Displays: 3.5
  console.log(average(['5'])); // Displays: 5
  console.log(average([])); // Displays: ERROR
  console.log(average([5, 'test'])); // Displays: NON NUMERIC
  console.log(average(45, 35)); // Displays: ERROR
}

The complete code of the "average" function:

// Average based on an array of values
function average(valueArray) {

  // Constant containing the number of values in the array
  const numValues = valueArray.length;

  // If the number of values in the array is 0 (or undefined), return an error message
  if (!numValues) {
    return 'ERROR';
  }

  // Variable that will contain the sum of the values
  let total = 0;

  // Loop based on the number of values in the array
  for (let i = 0; i < numValues; i++) {

    // Conversion to number
    const number = Number(valueArray[i]);

    // If non-numeric
    if (isNaN(number)) {
      return 'NON NUMERIC';
    }

    // Add the value to the total
    total += number;
  }

  // Returns the average, i.e., the sum of the values divided by the number of values
  return total / numValues;
}