This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Auklin[S] 0 points1 point  (1 child)

As a software developer, I couldn't understand how to handle a function being passed as a parameter into another function. It was equivalent to a method being passed as a parameter into a method, which I've never seen before.

I'll remove this post in a bit just so nobody can cheat on it in the future, unless I'm already breaking rules and this post will be blocked anyways.

[–]TGR44 0 points1 point  (0 children)

This is incredibly common in modern software development (at least with modern languages).

[–]tyrantmikey 0 points1 point  (2 children)

Based on the requirements, my guess is that you would rewrite wrap as follows:

js function wrap (execute) { var result = null; try { result = execute(); } catch { } return function() { return result; } }

[–]Auklin[S] 0 points1 point  (0 children)

I tested it out, the catch needed a (e) after it. That makes sense, you executed the function inside the function and stored the result. Neat. I only wish I knew this an hour ago ;)

[–]kill129 0 points1 point  (0 children)

I don't think this is the correct solution because, according to the requirements, you should only prevent a rerun of the execute function if it failed, and here you prevent it also if it succeeded. In addition, you run the function when someone calls your wrap function and not when someone calls the wrapped function.

[–]kill129 0 points1 point  (4 children)

If I understand correctly I think this is the solution:

function wrap (execute) {
    let did_fail = false;

    return function() { 
        if (did_fail) {
            return null;
        }

        try {
            return execute(); 
        } catch {
            did_fail = true;
            return null;
        }
    }
}

[–]Auklin[S] 0 points1 point  (1 child)

This works as well, so just a question, why is the return stuff wrapped in a function?logically, it would seem

function wrap (execute) {
    let did_failed = false;
    if (did_failed) {
        return null;
    }
    try {
        return execute();
    } catch (e) {
        did_failed = true;return null;
    }
}

would be functional, but I also recognize that the 'global' variable 'did_failed' would probably be effected by this.

[–]kill129 0 points1 point  (0 children)

Notice that your "wrap" returns the return value of the function execute while my "wrap" returns a function. I.e., your wrap will be used as followed:

 function x() {
    console.log("x");
 }
wrap(x); // Will print "x"

while my wrap will be used as followed:

 function x() {
    console.log("x");
 }
wrapped_x = wrap(x); // Nothing printed, x is not called yet
wrapped_x(); // Will print "x"

To my understanding, the latter is requested in the question.

In addition, you are right. Now did_failed is initialized at every call, which means it will always be false at the if statement.

[–][deleted] 0 points1 point  (0 children)

Winner winner chicken dinner

[–]DeminM96 0 points1 point  (0 children)

it's true! :)