Which Function Returns the Larger Number?

Published by bangyen in

Your function will be passed two functions, f and g, that don't take any parameters. Your function has to call them, and return a string which indicates which function returned the larger number.

  • If f returns the larger number, return the string f.
  • If g returns the larger number, return the string g.
  • If the functions return the same number, return the string neither.

Examples

// returns anonymous functions which return a particular number

function generate($n) {
  return function() use ($n) {return $n;};
}

larger(generate(5), generate(10)) ➞ "g"

larger(generate(25), generate(25)) ➞ "neither"

larger(generate(505050), generate(5050)) ➞ "f"

Notes

This exercise is designed as an introduction to higher order functions (functions which use other functions to do their work).

Watch a quick demo on how Edabit works.