You can also pass the result of one function
as a parameter to another, for example, this
is how we first find the square of the number
2
, and then the square of the result:
function func(num) {
return num ** 2;
}
let res = func(func(2));
console.log(res); // shows 16
The functions, of course, do not have to be the same. Let, for example, we have a function that returns the square of a number, and a function that returns the cube of a number:
function square(num) {
return num ** 2;
}
function cube(num) {
return num ** 3;
}
Let's use these functions to square the number
2
, and then cube the result of this operation:
let res = cube(square(2));
console.log(res);
Suppose now we have a function that returns the square of a number, and a function that finds the sum of two numbers:
function square(num) {
return num ** 2;
}
function sum(num1, num2) {
return num1 + num2;
}
Using these functions, we find the sum of
the square of the number 2
and the
square of the number 3
:
let res = sum(square(2), square(3));
console.log(res);
Suppose you have a function that returns the square root of a number, and a function that rounds a fraction to three decimal places:
function sqrt(num) {
return Math.sqrt(num);
}
function round(num) {
return num.toFixed(3);
}
Use these functions to find the square
root of 2
and round it to three
decimal places.
Suppose you have a function that returns the square root of a number and a function that returns the sum of three numbers:
function sqrt(num) {
return Math.sqrt(num);
}
function sum(num1, num2, num3) {
return num1 + num2 + num3;
}
Using these functions, find the sum of
the roots of the numbers 2
, 3
and 4
and write it into
the variable res
.
Suppose you have a function that rounds a fraction to three decimal places:
function round(num) {
return num.toFixed(3);
}
Use this function to modify the previous
task so that the variable res
is
set to a fraction rounded to 3
decimal places.