0
Conditional operators
So in a Syntax like this one var age = 42; var isAdult = (age < 18) ? "Too young": "Old enough"; document.write(isAdult); the first option has the value "false" and the second the "true" one. I tried to change the places of "Too young" and "Old enough" and it shown me "Too young". Is my opinion right?
7 RĂ©ponses
0
Assignments x=y are almost always true.
Your example in human format:
If spec is 0 then return loser else return winner
If (spec==0) then (return âLoserâ) else (return âWinnerâ)
Same as:
(spec ==0)?âLoserâ:âWinnerâ
If you used spec = 0 then that almost always return true (!=0)
+ 2
in (..) you have a statement, then '?' ask the statement: "Is it true or false?", if its true the code will execute the first part of the condition(before colon), otherwise it will execute second part
+ 1
spec == 0, change)
it checks if spec == 0, and if its true is will be equal to "winner"
+ 1
the '?' asks "Is spec equal to zero?" and the answer is true so the code will execute first part(spec will be equal to "winner")
0
Well, I'm angry on my brother and I wrote this cute programm for him.
var Chris = "Chris is a ";
var spec = 0; // if it's zero, then Chris* is a loser
var is =(spec = 0)?"Winner":"Loser";
document.write(Chris + is);
As (spec = 0) is true, and Chris is a loser, I must put the "Loser" option on the second place. This means following:
(true)?"false":"true" = true
(false)?"false":"true" = false
It's also logical to do that. It's beginning with a zero and not with a one.
*name changed :D
0
What?!, O.o
Why?
0
My mind is too stupid for this đđ
Thanks for explanation :)