0

The jsfiddle.net code below produces Uncaught SyntaxError: Unexpected token ( and the text "Here", but does not produce the result I wish to study, which will help me understand callbacks in functions and how their return value, true/false in my case, can be used to determine which of two things are to be computed.

I would like help removing the syntax error so I can see the result.

function checkPos(input, callback) {
    if (input > 0)
    function () {
        callback(true);
    } else
    function () {
        callback(false);
    }
}

function dosomething(number) {
    var diff = 10 - number;
    checkPos(diff, function (e) {
        return e;
    });
}

var count = 1;
var multiplier = 6;
var result;
if (dosomething(11)) result = 5 + count
else result = 5 - count

document.getElementById("demo").innerHTML = String(result*multiplier);

The html code is simply as follows.

<body>Here
    <p id="demo"></p>
</body>
6
  • What are these function keywords doing there in checkPos? I mean, what are they supposed to do there? Commented Jul 20, 2014 at 20:03
  • The return value of a callback function is returned to where it was called, not where the function was defined. Your doSomething won't work. Commented Jul 20, 2014 at 20:04
  • Yes, as you can see, I don't know how to fix that. Do you know how? Commented Jul 20, 2014 at 21:31
  • Give doSomething a callback parameter as well, which you call from the other callback. See also stackoverflow.com/q/14220321/1048572 and possibly stackoverflow.com/q/23667086/1048572. If checkPos is not asynchronous, remove the callback style altogether and just return. Commented Jul 20, 2014 at 21:49
  • You are right in suggesting to remove the callback-style at all. But you are wrong in assuming, some method has to be asynchronous to use callbacks; although it makes more sense, if it were. Commented Jul 20, 2014 at 22:29

3 Answers 3

3

What you wrote is

function checkPos(input, callback) {
    if (input > 0)
    function () {
        callback(true);
    } else
    function () {
        callback(false);
    }
}    

That is a Syntax Error. You could not define a function in the midst of an if-statement

But you can call your callback:

function checkPos(input, callback) {
    if (input > 0)
        callback(true);
    else
        callback(false);
}

Here is the updated Fiddle

Sign up to request clarification or add additional context in comments.

9 Comments

Something is apparently wrong. I always get the result 24, and never 36, which would be the result if 10-number were positive, for example, if number were -3. What's wrong?
Your function doSomething is wrong. Especially this line: checkPos(diff, function (e) { return e; });
Right. So how do I get the true/false result back where I need it?
Maybe try returning something ... return checkPos(diff, function (e) { return e; });
Oh, you pointed to the right point:return checkPos(diff, function (e) { return e; }); is one half and checkPos should return the result of the callback. That should do the trick. jsfiddle.net/LgcaX/4
|
0

You need to name your functions if they are not attached to a variable ex.

function()
//not okay
var abc=function()
//acceptable

1 Comment

That is not the problem here: defining an anonymous function in an if-statement.
0

if/if-else statement in javascript/ecmascript has the form:

if ( Expression ) Statement else Statement

if ( Expression ) Statement

You used function expression in statement's location, which according to the grammar isn't correct and hence the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.