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:
- any result returned by the function execute should be returned from the modified function unchanged.
- if the function execute throws a error, the modified function should return null.
- after the function execute has thrown an error, its future executions should be prevented and null should be returned
- 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"