-1

I'm looking at the following Javascript code, trying to port it to another language:

if (x>n) {return q} {return 1-q)

I don't know what the code is doing, but can someone tell me based on syntax what occurs? Is there an implied 'else' before the last set of {}? That is, if x>n then return q otherwise return 1-q?

If it helps, here's the line of code it was embedded within:

if(x>9000 | n>6000) { var q=Norm((Power(x/n,2/5)+1/(8*n)-1)/Sqrt(9/(2*n)))/3; if (x>n) {return q}{return 1-q} }

thanks

6
  • @Jacob Relkin It could be from a compressed java script library i.e. thrown out all spaces and newlines to minimize file size. Commented Feb 17, 2011 at 0:38
  • 1
    @SemVanmeenen Except it hasn't thrown out all meaningless spaces :P Commented Feb 17, 2011 at 0:40
  • Unless I'm unaware of something it looks to me like that code is broken. Pop open firebug console: if (1>2) {1} {2} prints 2, all good. if (3>2) {1} {2} prints 2... OK...? if (3>2) {1} else {2} prints 1. Commented Feb 17, 2011 at 0:42
  • yah, the whole code base is like that. Commented Feb 17, 2011 at 0:42
  • @Alex Oh, right. Just glanced over it. Commented Feb 17, 2011 at 0:43

6 Answers 6

2

There is not an implied 'else', the next set of braces simply defines another compound statement. In this case, it acts like an 'else', but only because the 'if' portion carries a return statement.

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

Comments

1

There's only an implied else because the block that pairs with the if statement returns from the function. The code you posted is equivalent to:

if(x > n)
{
    return q;
}
return q + 1;

You only get if/else-like behaviour because the second statement can only be executed if the first statement isn't (because the first statement would return from the function, and control would never get to the second).

This bad code! For readability / maintainability / sanity, it should be written in one of these more sustainable formats:

if(x > n) {return q;}
else      {return q + 1;}

Or:

return (x > n)? q : q + 1;

Hope that helps!

Comments

1

Its basically this:

if (x>n)
   return q; 
else 
   return 1-q;

4 Comments

beautifully answered. Simple as.
Not true! It only happens to work because the if block returns. Anything else, say if(x > n) {x += 12} {x +=7}, will behave completely differently. There is not an implied else.
@Xavier - it is with the return.
True, but I think this is a dangerous answer to the more general question of "Does if(x) {block-1} {block-2} evaluate as if(x) {block-1} else {block-2}?" You're right in this specific case, but making that assumption globally (which your answer seems to propound) will bite you as soon as block-1 doesn't have a return statement.
1

There's an implied else. If x>n, the function returns q. Otherwise, it goes to the next return statement and returns 1-q.

2 Comments

Slightly misleading to say "implied else". If there was no return in the if clause, both clauses would be executed when the test evaluates to true. See jsfiddle.net/daybarr/jJCGE for code
With that first return there, the second return would only be reached if the if statement was false. Hence, the implication of an else. It's stylistically bad, but what he was asking for was the meaning - what would happen if the code executed. I hope his rewrite would be better code. ;-)
1

That block of {} after return q is not necessary. there isn't any else statement implied there, it's just that if it passes throught the if statement and return q, what comes next won't be executed anymore, otherwise, it will. Then there is no need to put an else there.

Comments

1

That code is really...

if (x > 9000 | n > 6000) {
    var q = Norm((Power(x / n, 2 / 5) + 1 / (8 * n) - 1) / Sqrt(9 / (2 * n))) / 3;
    if (x > n) {
        return q
    } {
        return 1 - q
    }
}

Either someone thinks unreadable code is a good idea, or they used a bad minifer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.