This is an SO challenge
I would like to know how someone would get an invalid formal parameters in a function without the arguments
object to as simulate not knowing the format of the parameter destructuring assignment. This is not an ECMAScript question and only pertains to JavaScript.
Your mySolution
cannot access arguments
or test
. You are provided with an args
array which contains the parameter names. You must return an object which has a property for every parameter which is the parameter that was passed to the function. In short, results[prop]
must === test[prop]
. Your solution shouldn't rely on bugs or security holes as they may not be present in the future. The solution to this problem of which I have in mind does not rely on any bugs.
(function () {
function mySolution ({
var,
this,
function,
if,
return,
true
}) {
// prohbit reference to arguments and the test object
var test = arguments = null,
args = ['var', 'this', 'function', 'if', 'return', 'true'],
results = {};
// put your solution here
return results;
};
var test = {
"var" : {},
"this" : {},
"function": {},
"if" : {},
"return" : {},
"true" : {}
},
results = mySolution(test),
pass = true;
for (var prop in test)
if (test.hasOwnProperty(prop))
if (results[prop] !== test[prop])
pass = false;
alert(pass ? "PASS" : "FAIL")
}());
Here's one of the two possible solutions that I would have accepted:
(function () {
function mySolution ({
var,
this,
function,
if,
return,
true
}) {
// prohbit reference to arguments and the test object
var test = arguments = null,
args = ['var', 'this', 'function', 'if', 'return', 'true'],
results = {};
var i = args.length;
while (i--) {
results[args[i]] = eval("function::" + args[i]);
// function::[args[i]] won't work unless you eval() it
}
return results;
};
var test = {
"var" : {},
"this" : {},
"function": {},
"if" : {},
"return" : {},
"true" : {}
},
results = mySolution(test),
pass = true;
for (var prop in test)
if (test.hasOwnProperty(prop))
if (results[prop] !== test[prop])
pass = false;
alert(pass ? "PASS" : "FAIL")
}());
The solution works by using the default function::
namespace in combination with eval()
scope.
For example: foo.function::bar
and foo.function::['bar']
are the same thing foo.bar
.