How it works this code?

I will be grateful if explain how the parameters become functions and why is confirm if it is only present in the condition.

Thank you!

function ask(question, yes, no) {
 if (confirm(question)) yes()
 else no();
}

function showOk() {
 alert( "You agreed." );
}

function showCancel() {
 alert( "You have cancelled the execution." );
}

// use
ask("do You agree?", showOk, showCancel);
March 23rd 20 at 19:32
3 answers
March 23rd 20 at 19:34
Solution
as the parameters become functions

They did not, they were. You pass as a parameter yesfunction showOk

why is confirm

Because you cause so confirm(question)
March 23rd 20 at 19:36
Solution
// here we accept functions as parameters (yes showOk no - showCancel)
function ask(question, yes, no) {
 // confirm(question) is because we need a result from if (true or false)
 // If confirm(question) is true (click OK), then performed yes() (showOk), otherwise run no() (showCancel)
 if (confirm(question)) yes()
 else no();
}

function showOk() {
 alert( "You agreed." );
}

function showCancel() {
 alert( "You have cancelled the execution." );
}

// now pass the function showOk and showCancel in the ask parameters (the first parameter is the message text to confirm)
ask("do You agree?", showOk, showCancel);
Thank you! - rick commented on March 23rd 20 at 19:39
March 23rd 20 at 19:38
In short - we have a function that takes three parameters - the message text and two callbacka.
The function displays a message with buttons "Yes" / "no" (it depends on the browser executes each message in its own way, the text of the buttons and location can also differ), and if the user tyknite Yes - pulls the first kolbek if tyknite no - jerks the second colback.
And covaci we pass two functions, each of which shows a message (different).

Find more questions by tags JavaScript