Skip to content

Instantly share code, notes, and snippets.

@szemate
Last active March 26, 2022 09:04
Show Gist options
  • Save szemate/66ded6ca47340508042a0a3c9caa7c2f to your computer and use it in GitHub Desktop.
Save szemate/66ded6ca47340508042a0a3c9caa7c2f to your computer and use it in GitHub Desktop.
Callbacks exercise

Callback function exercise

We are creating a calculator that can do three types of calculation:

  • doubling a number with the double function
  • halving a number with the halve function
  • squaring a number with the square function

Complete the functions so that calculate takes one of the functions that implement an operation and a number as arguments, and returns the result of the calculation.

function double(x) {
  // write solution here
}

function halve(x) {
  // write solution here
}

function square(x) {
  // write solution here
}

function calculate(/* write parameters here */) {
  // write solution here
}

// Examples
console.log(calculate(double, 4)); // 8
console.log(calculate(halve, 4)); // 2
console.log(calculate(square, 4)); // 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment