Tag: objects

FCC Bonfire Series 139: Make a Person

Today, we are going to be making a person (not in that sense, you) using a constructor function. When this function get’s called using the new keyword, it will construct and return an object, that’s why […]

FCC Bonfire Series 127: Sorted Union

This next bonfire challenge is a nice way of getting to know the arguments object. The arguments object is a local variable available within any function and contains an array of the arguments provided. Here’s an example:

function foo() {
  for (var i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}

foo('b', 'a', 'r', [0, 1, 2]); //-> b, a, r, [0, 1, 2]

The is something that must be on our minds at all times, although the arguments object seems to be an array and even has a length method (and callee and caller, you can read about them here) it actually isn’t, so no Array methods are available within it (say filter, reduce, forEach, etc).

Now that that’s out of the way, let’s get to it. We must write a function that given any number of arrays as arguments, it returns a single array containing unique values in the same order as the originally provided arrays:

unite([5, 6, 7], [2, 3, 5]); //-> [5, 6, 7, 2, 3]
unite([1, 2, 3], [5, 2, 1, 4], [2, 1]); //-> [1, 2, 3, 5, 4]

There are quite a few ways of getting this done, we’ll go for the most simple solution, and I will let you come up with something else.