-2

I have this code:

(options.parent) ? top = options.parent.height / 2; : top = parent.height() / 2;

Because of the double slash that i use there it get's an error i know why i get the error i just don't know how to write it to work.
Thanks again.

Anghel Gabriel
  • 55
  • 1
  • 1
  • 6
  • 5
    What double-slash? Maybe my eyes are failing me. – Tony Day Jan 11 '13 at 12:44
  • 1
    This code, even fixed, doesn't look that something I'd like to have in my source. It's not the most readable one. Please use if/else or simply `top = condition ? value 1 : value 2;` – Denys Séguret Jan 11 '13 at 12:46
  • @TonyDay, yes, it's not double slash, it's the semicolon in the middle. – gdoron Jan 11 '13 at 12:48
  • How did you manage to run your code? When you try to run it is gives you an error in the console: `SyntaxError: missing : in conditional expression`, which should have indicated the issue. – Nope Jan 11 '13 at 12:54
  • 1
    Voting to close as too localised due to typo in code. – J. Steen Jan 11 '13 at 12:54

2 Answers2

1

It has nothing to do with the "double slashes", remove the semicolon

(options.parent) ? top = options.parent.height / 2 
                 :top = parent.height() / 2;

Semicolon was meant to define end of statement(optional), the ternary operator is treated as a single statement.

(options.parent) ? top = options.parent.height / 2; : top = parent.height() / 2;
//                                                ^------ Wrong!
gdoron
  • 147,333
  • 58
  • 291
  • 367
0

I think it's not the slash problem but the semicolon. Try this:

top = (options.parent ? options.parent.height : parent.height()) / 2;
Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53