Diane Foster Diane Foster - 2 months ago 7
Javascript Question

Using a function as a parameter to another function

"use strict";
function square(x) {
return x * x;
}
function add(square,y) {
return square(4) +y
document.write(square + y)
}
add(4,1);
console.log(add(4,1));


I have to use a function as in the parameter of another function. I am trying to output 17.

Answer

Just call it as such:

add(square,1);

you may also want to change

function add(square,y) {
    return square(4) +y
    document.write(square + y)
}

to be:

function add(square,y) {
    document.write(square(4) + y)        
    return square(4) +y
}

Per Andrew's comment, it might also be a good choice to change document.write calls to be console.log and open the browser console to read results.

Comments