0

I recently gave an online JS test, the questions was below, I couldn't understand and solve that, but now I want how that can be done. Please help me understand that.

write a wrap function that takes the execute function as an argument and returns a modified function so that the following requirements are met:

  1. any result returned by the function execute should be returned from the modified function unchanged.
  2. if the function execute throws a error, the modified function should return null.
  3. after the function execute has thrown an error, its future executions should be prevented and null should be returned
  4. multiple modified functions can co-exist

The execute function doesnt take any argument.

function wrap (execute) {
  // Return modified function
}

var errorExec = wrap(function () {
  throw new Error('Error');
});
var resultExec = wrap(function () {
  return "Result";
});
console.log(errorExec && errorExec()); // Should output null
console.log(errorExec && resultExec()); // Should output "Result"
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
raju
  • 6,448
  • 24
  • 80
  • 163

0 Answers0