Rahul Sharma
Rahul Sharma

Reputation: 11

Getting undefined for this callback function

prepOutput() is a simple function that takes an average and returns the callback output(), but I am getting an undefined error at "return callback(out)". Why is the callback not working?

    function finish() {
        isRunning = false;
        prepOutput(avgSpeed(),output());

    }

    function avgSpeed() {
        var avg = (totReactt/numClick);
        return avg.toFixed(2);
    }

    function prepOutput(avgS, callback){
        var out = "Your averege speed is " + avgS;

        return callback(out);

    }

    function output(x) {
        alert("Thank you for playing! " + x);
    }

Upvotes: 0

Views: 110

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to pass a function reference as the value for callback, instead you are invoking output and is passing the value returned from it(undefined as there is no value returned) as the value for callback argument to prepOutput

prepOutput(avgSpeed(), output);

Problem: Demo - output is called before prepOutput
Demo: Fiddle

Upvotes: 3

Related Questions