Functions With Array Arguments

Functions can take an array of elements as an argument.

function adder(numbers) {
  var result = 0;
  for (var i = 0; i < numbers.length; i++) {
    result += numbers[i];
  }
  return(result);
}

adder([1, 4, 9]); // 14
adder([10, 20]); // 30
adder([55]); // 55

The adder function is cool because it can sum the numbers in an array of any length.

We can use to adder function to calculate the average of the numbers in an array of any length.

function averager(numbers) {
  var result = adder(numbers) / numbers.length;
  return(result);
}

averager([1, 4, 9]); // 4.66
averager([10, 20]); // 15
averager([55]); // 55

The following funify function will add the string "is fun!" to each element in an array.

function funify(words) {
  var result = [];
  for (var i = 0; i < words.length; i++) {
    result.push(words[i] + " is fun!");
  }
  return(result);
}

funify(["math", "programming"]); // ["math is fun!", "programming is fun!"]

The funify function iterates over every element in the words array and adds "is fun!" to each array element.

Question Click to View Answer

What does the following code print to the console?

function maxFinder (numbers) {
  var max = numbers[0];
  for (var i = 0; i < numbers.length; i++) {
    if (max < numbers[i]) {
      max = numbers[i];
    }
  }
  return(max);
}

console.log(maxFinder([100, 40, 99]));

100

The maxFinder() method iterates over all elements in an array of numbers and returns the largest number.

What does the following code print to the console.

function likeify(words) {
  var result = [];
  for (var i = 0; i < words.length; i++) {
    result.push("i like " + words[i]);
  }
  return(result);
}

var r = likeify(["math", "programming"]);
console.log(r);

["i like math", "i like programming"]

The likeify function iterates through every element in the words array and prepends "i like" to each string.

<-- Previous (Type Conversion) Next (Modulus Operator) -->