← Back to challenges

Sort by Number of Calls

JavaScriptHardclosureshigher_order_functionsfunctional_programming

Instructions

Create a function that takes a list of functions and sorts them in ascending order based on how many calls are needed for them to return a non-function.

Examples

f1 = _ => "hello"
// f1() ➞ "hello"

f2 = _ => _ => "innokodakademija"
// f2()() ➞ "innokodakademija"

f3 = _ => _ => _ => "user"
// f3()()() ➞ "user"

funcSort([f2, f3, f1]) ➞ [f1, f2, f3]
// [f2, f3, f1] ➞ [2, 3, 1] ➞ [1, 2, 3] ➞ [f1, f2, f3]

funcSort([f1, f2, f3]) ➞ [f1, f2, f3]
// [f1, f2, f3] ➞ [1, 2, 3] ➞ [1, 2, 3] ➞ [f1, f2, f3]

funcSort([f2, "func"]) ➞ ["func", f2]
// [f2, "func"] ➞ [2, 0] ➞ [0, 2] ➞ ["func", f2]

Notes

  • Treat non-functions as needing zero calls.
  • Every function will be called with empty parameters.
  • Every function will need to be called at least once.
  • The potentially returned values include Numbers, Booleans, and Strings, among others.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.