Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Callback Question? #27

Open
mahmoudAcm opened this issue Dec 24, 2022 · 1 comment
Open

Custom Callback Question? #27

mahmoudAcm opened this issue Dec 24, 2022 · 1 comment

Comments

@mahmoudAcm
Copy link

mahmoudAcm commented Dec 24, 2022

I'm kinda confused it says to add two arguments so why do I do this manually in the code below

should it be like the code below?

const addTwoArgs = (a, b, callback) => {
  if (callback && typeof callback === "function") {
    return callback(a, b);
  }
};

let result = addTwoArgs(2, 5, (a, b) => {
  return a + b;
});
console.log(result);

If I am wrong please correct me.

@AtahanKocc
Copy link

You have a function addTwoArgs, which takes two arguments a and b, along with a third argument callback. The addTwoArgs function checks if the callback is a valid function using typeof and then proceeds to call the callback function passing the arguments a and b.

When you call addTwoArgs(2, 5, (a, b) => { return a + b; }), it adds the two arguments 2 and 5 by invoking the callback function (a, b) => { return a + b; }. In this case, the callback function simply adds the two arguments and returns the result.

So, the output of console.log(result); will be 7, which is the result of adding 2 and 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants