4 kyu

Default Arguments

1,256 of 1,330xcthulhu

Description:

Write a function defaultArguments. It takes a function as an argument, along with an object containing default values for that function's arguments, and returns another function which defaults to the right values.

You cannot assume that the function's arguments have any particular names.

You should be able to call defaultArguments repeatedly to change the defaults.

function add(a,b) { return a+b;};

var add_ = defaultArguments(add,{b:9});
add_(10); // returns 19
add_(10,7); // returns 17
add_(); // returns NaN

add_ = defaultArguments(add_,{b:3, a:2});
add_(10); // returns 13 now
add_(); // returns 5

add_ = defaultArguments(add_,{c:3}); // doesn't do anything, since c isn't an argument
add_(10); // returns NaN
add_(10,10); // returns 20
add = (a, b) -> a + b

add_ = defaultArguments(add, {b: 9})
add_ 10 # returns 19
add_ 10, 7 # returns 17
add_() # returns NaN

add_ = defaultArguments(add_, {b: 3, a: 2})
add_ 10 # returns 13 now
add_() # returns 5

add_ = defaultArguments(add_, {c:3}) # doesn't do anything, since c isn't an argument of add_
add_ 10 # returns NaN
add_ 10, 10 # returns 20

HINT: This problem requires using Fuction.prototype.toString() in order to extract a function's argument list

Functional Programming
Metaprogramming

Stats:

CreatedOct 17, 2013
PublishedOct 17, 2013
Warriors Trained8466
Total Skips3056
Total Code Submissions44886
Total Times Completed1330
JavaScript Completions1256
CoffeeScript Completions78
Total Stars380
% of votes with a positive feedback rating82% of 223
Total "Very Satisfied" Votes160
Total "Somewhat Satisfied" Votes46
Total "Not Satisfied" Votes17
Ad
Contributors
  • xcthulhu Avatar
  • dnolan Avatar
Ad