# What do the values in the function?

``````var a = +prompt("Enter the first number","");
var operation = prompt("Enter the operation","+, -, /, *, ^");
var b = +prompt("Enter the second number","");
if (!parseInt(a) || !parseInt(b)) {
alert ("You have entered an invalid number");
}

function sum(a,b) {
return a + b;
}

function subtract(a,b) {
return a - b;
}

function multiply(a,b) {
return a * b;
}

devizion function(a,b) {
if (b === 0){
alert("zero will not be divided!")
}
else {
return a / b;
}

}

function exponentiation(a,b) {
return Math.pow(a,b);
}

function calculate(cb, a, b) {
var result = cb(a, b);
document.write("Result:" + result);
}

switch (operation) {
case "+":
calculate(sum, a, b);
break;
case "-":
calculate(subtract, a, b);
break;
case "*":
calculate(multiply, a, b);
break;
case "/":
calculate(devizion, a, b);
break;
case "^":
calculate(exponentiation, a, b);
break;
}``````

What do function calculate(cb)
and why is it so? they never show up and it is not variables like

var result = cb(a, b);
June 27th 19 at 15:27
June 27th 19 at 15:29

I'll try to sort through.

``````var a = 1, b = 2; // these a, b – a "global" above all, "seen" everywhere
function calculate(x, y, z) {
// this defines the local variables x, y, z
// and are visible all the variables from the levels above
// eg. a and b
// expect x – this will be a function of
return x( y+a, z+b); // you can use local and global
}

function sum( a, b){ // and here a,b are redefined locally
// and to the global a and b have no relationship!
return a+b; // adds a and b that gave the next call to sum()
}

calculate( sum, 5, 6); // call the calculate and there will be x = sum, y=5, z=6
sum( 7, 8); // call sum directly, inside of which is a=7 and b=8
// the global a and b does not suffer.``````
June 27th 19 at 15:31
they never show up and it is not variables like

Who are THEY?
``````var a = +prompt("Enter the first number","");
var b = +prompt("Enter the second number","");``````

What do function calculate(cb)

Calls to the necessary function for the arguments a and b and prints the result.
mean c and b - Richard.Bechtelar commented on June 27th 19 at 15:34
June 27th 19 at 15:33
In this case, cb is an abbreviation for callback.
Depending on the selected operation in the calculate function is passed callback and arguments.
June 27th 19 at 15:35
Here is the function call
`calculate(sum, a, b);`
Here's the function itself
``````function calculate(cb, a, b) {
var result = cb(a, b);
document.write("Result:" + result);
}``````

Passed three arguments, got 3 arguments. What confuses You? The fact that the 1st argument is a function?
mind the fact that cb never declared, has nothing in it, or it is not assigned to any variable, and since the switches(magically) transmitted to him an expression. Fantastic! - Richard.Bechtelar commented on June 27th 19 at 15:38
What we have stated in the function values I can understand, but how it knows what value to take from switch - I'll never know - Richard.Bechtelar commented on June 27th 19 at 15:41
:
in cb function, namely:
``````function sum(a,b) {
return a + b;
}``````

What is not declared? If it is in Your code - Richard.Bechtelar commented on June 27th 19 at 15:44
That the sum function is called, not cb - Henderson_Beat commented on June 27th 19 at 15:47
Well, here is the function call
`calculate(sum, a, b);`
1 argument hits cd, the second in a, third in b
So it is possible and so:
``````function calculate(zopa, vasya, petya) {
var result = zopa(vasya, petya);
document.write("Result:" + result);
}``````

nothing will change - Richard.Bechtelar commented on June 27th 19 at 15:50
: goes to cb is assigned a value depending on which operator people entered? - Henderson_Beat commented on June 27th 19 at 15:53
`function calculate(zopa, vasya, petya)` - in brackets, are declared variables visible inside the function, which will be assigned the values passed to the function. Naming does not matter, order. - Richard.Bechtelar commented on June 27th 19 at 15:56
: Thank you, understood) - Henderson_Beat commented on June 27th 19 at 15:59

Find more questions by tags JavaScript