Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created October 7, 2014 08:41
Show Gist options
  • Save alanshaw/60faf84f2e6b596895e2 to your computer and use it in GitHub Desktop.
Save alanshaw/60faf84f2e6b596895e2 to your computer and use it in GitHub Desktop.
Exercise questions covering arguments, arrays, functions, call, apply and closures
/*
Create a module that logs out arguments to a function on separate lines.
I should be able to call the module as follows:
*/
var argLogger = require('./arg-logger');
argLogger('foo', 3, {}, function () {});
// Expected output:
// foo
// 3
// {}
// [Function]
/*
Create a module that returns the first n arguments passed to the function as an
array where n is the first parameter of the function.
I should be able to call the module as follows:
*/
var argSlicer = require('./arg-slicer');
var fourArgs = argSlicer(4, 'foo', 'bar', 'baz', 'boz', 'ben');
console.log(fourArgs);
// Expected output:
// [ 4, 'foo', 'bar', 'baz' ]
/*
Create a module that takes a number n as its only parameter. It should return a
function that returns the first n args it is given as an array.
I should be able to call the module as follows:
*/
var argChopperFactory = require('./arg-chopper-factory');
var chopper = argChopperFactory(2);
var twoArgs = chopper('foo', 'bar', 'baz');
console.log(twoArgs);
// Expected output:
// [ 'foo', 'bar' ]
/*
Create a module that joins two sets of arguments together. It should return a
function that returns an array containing all the arguments passed to the
module joined with the arguments passed to the returned function.
I should be able to call the module as follows:
*/
var argMungerFactory = require('./arg-munger-factory');
var munger = argMungerFactory(1, 2, 3, 4);
var mungedArgs = munger(5, 6);
console.log(mungedArgs);
// Expected output:
// [ 1, 2, 3, 4, 5, 6 ]
/*
Create a module that returns a function. When calling the module you should
be able to pass it:
1) A function - f
2) A context object - c
3) A variable number of further arguments - arg1, arg2, ...
When the function your module returns is called, three things should happen:
1) It should call f
2) `this` when referenced within f, should be set to c
3) The arguments f receives should be arg1, arg2, etc.
I should be able to call the module as follows:
*/
var funkall = require('./funkall');
var f = function () { console.log(this, arguments); };
var c = {foo: 'bar'};
var funked = funkall(f, c, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
funked();
// Expected output:
// { foo: 'bar' } { '0': 1,
// '1': 2,
// '2': 3,
// '3': 4,
// '4': 5,
// '5': 6,
// '6': 7,
// '7': 8,
// '8': 9,
// '9': 10 }
/*
Create a module that returns a function. When calling the module you should
be able to pass it:
1) A function - fn
2) A mapping function - mapFn
When the function your module returns is called, three things should happen:
1) A new set of arguments should be created by passing each argument to mapFn
and using the returned value
2) fn should be called, and it should be passed the new set of args as params
I should be able to call the module as follows:
*/
var funArgMapper = require('./fun-arg-mapper');
var fn = function () { console.log([].slice.call(arguments)) }
var mapFn = function (arg) { return arg * 2 }
var doubler = funArgMapper(fn, mapFn);
doubler(2, 8, 32);
// Expected output:
// [ 4, 16, 64 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment