Intermediate JavaScript - More Functions

Question Click to View Answer

What does the following code print to the console?

function dog () {};
console.log(dog.name);
"dog"

Functions have a name property that corresponds with the name of the function.

What does the following code print to the console?

var boo = function () {};
console.log(boo.name);
""

Anonymous functions also have a name property, but it returns the empty string.

Write a recursive factorial function and then verify that the factorial of 4 equals 24.

function factorial(n) {
  if (n == 0) { return  1; }
  return n * factorial(n - 1);
}
console.log(factorial(4));

What does the following code print to the console?

var happy = {
  hi: function sing(n, result) {
    result = typeof result !== 'undefined' ? result : [];
    if (n == 0) {
      result.push("No more bottles");
      return result;
    }
    var str = n + " bottles";
    result.push(str);
    return sing(n - 1, result);
  }
}

console.log(happy.hi(3));
["3 bottles", "2 bottles", "1 bottles", "No more bottles"]

The happy object has a hi property that points to the named function sing(). Object values are typically anonymous functions, but they can also be named functions. A named function works well in this example to allow for recursion.

What does the following code print to the console?

function dinero () {};
dinero.meaning = "money";
console.log(dinero.meaning);

Functions are objects and can be assigned properties, just like any other object.

Describe what happens when the Helpers.factorial(3) function is run twice.

var Helpers = {
  cache: {},
  factorial: function (n) {
    if (this.cache[n]) { return this.cache[n]; };
    var counter = 1;
    var result = 1;
    while (counter <= n) {
      result *= counter;
      counter++;
    }
    this.cache[n] = result;
    return result;
  }
}

Helpers.factorial(3)
Helpers.factorial(3)

The factorial is cached in an object, so it does not need to be recomputed if it has already been calculated. When Helpers.factorial(3) is initially run, the factorial is computed and cached. When Helpers.factorial(3) is run again, the cached value is returned without performing the computations again.

Caching requires extra memory and makes the code more complex, but it is a very useful tactic for expensive calculations. Caching is also referred to as memoization.

Write a function called biggestArg() that takes a variable number of integer arguments and returns the largest integer. biggestArg(1, 3, 33, 4) should return 33.

function biggestArg () {
  return Math.max.apply(Math, arguments)
}

We don't need to use Math as the first argument for the apply() function, but it makes the code more readable. This code would also work:

Math.max.apply("", arguments)

Write a function called mergeObjects() that takes a variable number of objects as parameters and returns a single object with all the properties from the individual objects. mergeObjects({some: "thing"}, {cool: "jerk", phat: "cow"}, {holy: "moly"}) should return {some: "thing", cool: "jerk", phat: "cow", holy: "moly"}.

function mergeObjects () {
  var result = arguments[0];
  for (var i = 1, l = arguments.length; i < l; i ++) {
    var arg = arguments[i];
    for (var key in arg) {
      result[key] = arg[key];
    }
  }
  return result;
}

Why doesn't the following code work?

function badSortArgs () {
  var result = arguments.sort(function (x, y) {
    return x - y;
  });
  return result;
}
badSortArgs(1, 44, 3);

arguments is an array-like object, but it's not actually an array. The sort() method is not defined for the arguments object.

Define a goodSortArgs() function that takes a variable number of integer arguments an returns a sorted array. goodSortArgs(1, 44, 3) should return [1, 3, 44].

function goodSortArgs () {
  var args = Array.prototype.slice.call(arguments, 0);
  var result = args.sort(function (x, y) {
    return x - y;
  });
  return result;
}

What does the following code print to the console?

function b (x, y, z) {};
console.log(b.length);
3

Functions have a length property that corresponds with the number of named parameters.

What does the following code print to the console?

function a () {};
console.log(a.length);
0

Functions that don't have any named parameters have a length of 0.