- #1
- 127
- 0
Code:
var square = function (x) {
return x * x;
};
var cube = function (x) {
return square * x;
};
This is my shot, its not right. Please indicate or suggest the correct answear. :)
Last edited by a moderator:
var square = function (x) {
return x * x;
};
var cube = function (x) {
return square * x;
};
Code:var square = function (x) { return x * x; }; var cube = function (x) { return square * x; };
This is my shot, its not right. Please indicate or suggest the correct answear. :)
var cube = function (x) {
return x * square(x);
};
#include <stdio.h>
int f1xf2(int (*pf1)(int), int (*pf2)(int), int v){return(pf1(v) * pf2(v));}
int returnv(int v){return(v);};
int square(int v){return(f1xf2(returnv, returnv, v));}
int cube(int v){return(f1xf2(returnv, square, v));}
int quad(int v){return(f1xf2(square, square, v));}
int main(int argc, char ** argv)
{
printf("square 2 = %3d\n", square(2));
printf("cube 3 = %3d\n", cube(3));
printf("quad 4 = %3d\n", quad(4));
return(0);
}
Returning to the problem. "When we call the square function as a part of the cube function, we still have to define the parameter associated with the square function" - a hint provided says thus. But how to write this assignment? I have never assigned a parameter for a function in the body of another function.
Any hints?
Should I assign a parameter the same way as a variable?
var square = function(x) { return x * x; }
var cube = function(f,x) { return x * f(x); }
cube(square,2);
Using a function as an argument to another function would work like this:
Code:var square = function(x) { return x * x; } var cube = function(f,x) { return x * f(x); } cube(square,2);
Though in this context I don't know why you'd want to pass in a function. I don't imagine you'd ever be swapping it out.
var square = function (x) {
return x * x;
};
var cube = function (x) {
return square(x) * x;
};
var f1xf2 = function(f1, f2, x) {return f1(x) * f2(x);}
var returnx = function(x) {return x;}
var square = function(x) {return x * x;}
var cube = function(x) {return f1xf2(returnx, square, x);}
I thought the idea was to use functions as parameters to other functions. I'm not sure if this syntax is correct for javascript, but it should look something like this:
Code:var f1xf2 = function(f1, f2, x) {return f1(x) * f2(x);} var returnx = function(x) {return x;} var square = function(x) {return x * x;} var cube = function(x) {return f1xf2(returnx, square, x);}