GetFunctions takes no arguments and returns an array, containing 2 functions con
ID: 3567522 • Letter: G
Question
GetFunctions takes no arguments and returns an array, containing 2 functions contained in the same closure. Make the closure contain 2 variables that the 2 functions can access. Initialize the first variable, v1, to 0 and initialize the second variable, v2, to an empty array. Store the return of GetFunctions in an array called funcs so that funcs[0] takes 2 numbers as arguments, arg1 and arg2. If the value of v1 is less than 3, add arg1 and arg2 and put the sum onto the end of the array v2 (otherwise, it won't change v2). Then have funcs[0] return v2. Have funcs[1] should take no arguments, but increment v1 by 1 and not return anything.
function GetFunctions() { first function in funcs, which can be referenced as funcs[0], must take exactly two numbers as arguments (arg1 and arg2). If the value of "counter" is less than 3, it should add (arg1 + arg2) and put that sum onto the end of the array "arr".
function GetFunctions(){
//code goes here
}
Example:
var funcs = GetFunctions();
outStr = funcs[0](1, 2) + "<br>"; //outStr = "3"
outStr = funcs[0](3, 4) + "<br>"; //outStr = "3, 7"
funcs[1](); // v1 = 1
funcs[1](); // v1 = 2
outStr = funcs[0](5, 6) + "<br>"; //outStr = "3, 7, 11"
funcs[1](); // v1 = 3
outStr = funcs[0](7, 8) + "<br>"; //outStr = "3, 7, 11"
Explanation / Answer
function GetFunctions(){
var v1= 0;
var v2 = new Array();
var funcs = [
function(a, b)
{
if(v1 < 3)
{
v2.push(a+b);
}
return v2;
},
function()
{
v1++;
}];
return funcs;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.