Write the body of GetFunctionForArrayPairs so that it takes a function func as a
ID: 3567554 • Letter: W
Question
Write the body of GetFunctionForArrayPairs so that it takes a function func as an argument and returns a new function. Input: The argument func can be assumed to be a function which takes exactly two numbers as input and returns exactly one number. Output: The function which is returned by GetFunctionForArrayPairs must take as Ant an array of numbers and return a new array which is the result of applying func to each consecutive pair of elements in the argument array. For instance, if the input array was [1,2,3], then func should be applied to the pairs (1,2), (2,3), and (3,1). (We wrap from the end of the array to the beginning.) Example: Assume that the function ManipulateTwoNumbers as defined in assignmentS.html was the argument to GetFunctionForArrayPairs. Then, if the function returned from GetFunctionForArrayPairs received the array [1,2,3,4,5] as input, it should return the array [2,6,12,20,5]Explanation / Answer
function GetFunctionForArrayPairs(func)
{
var numbers = [1,2,3];
return func(numbers);
}
Function array[][] func(someArray)
{
var i = 0,j=0;
var n = (someArray.length * (someArray.length-1))/2
var x = new Array(10);
for (i = 0; i < n; i++)
{
x[i] = new Array(2);
}
for(i=0; i<funcArray.length; i++)
{
for(j=0;j<someArray.length;j++)
{
if(i!=j)
{
x[i][0] = i;
x[i][1] = j;
}
}
}
return x;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.