Tag: arguments object

Read More
ECMAScript Part 2 Featured image

ECMAScript 6: Why should I use it? | Part II

Welcome to part two of ECMAScript 6: Why Should I use it? If you missed part one, you can find it here. This time, we’ll be looking at the following features that ECMAScript 6 brings to the developer:

FCC Bonfire Series 138: Arguments Optional

Arguments Optional, oh boy, this is the last challenge before we get to the advanced bonfires section. This bonfire will show us some very interesting facts about JavaScript functions.

For this task, we are asked to write a function that accepts either one or two arguments. If two arguments are provided (and both are numbers), we proceed to return the sum; but, if we are only given a single argument, we must return a function. This returned function will accept one number as argument, and will return the sum of it and the originally passed number. Let’s see a few examples:

addTogether(5, 6);      //-> 11
add(11, 53);    //-> 64

var addFive = addTogether(5);
addFive(6);     //-> 11

var addEleven = addTogether(11);
addEleven(53);  //-> 64

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++) {

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.