Write the body of getMax using JAVASCRIPT so that it takes two functions as argu
ID: 3761793 • Letter: W
Question
Write the body of getMax using JAVASCRIPT so that it takes two functions as arguments and returns a new function. Input: Arguments f1 and f2 are assumed to be functions which each take a single integer argument and return a single integer. Output: The function returned by getMax must take a single argument, an integer (which we will call 'i'), and return the maximum of f1(i) and f2(i). Examples: Given the definitions of add2 and mult4 at the bottom of this page, if var fx = getMax(mult4,square), then fx(2) = 8, fx(3) = 12, fx(4) = 16, and fx(5) = 25.
Explanation / Answer
Answer :
function f1(value)
{
var a = parseInt(value);
alert(a);
}
function f2(value)
{
var b = parseInt(value);
alert(b);
}
function getMax(v)
{
var square=v*v;
var t= 4*v;
var max= Math.max(t, square);
alert(max);
}
function myFunc(callback, args)
{
callback.apply(this, args);
}
myFunc(f1, [10]);
myFunc(f2,[20]);
myFunc(getMax,[5]);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.