+ 8

Tutorial : simple if else statement (ternary operator) - MAKE IT EASY 📌

Hello I wanted to share a trick. https://code.sololearn.com/Wirdh4rMXimn/ if(foo === 0) { foo+= 1 ; } else { foo+= 2 ; } = foo===0 ? foo+=1 : foo+=2 condition ? execute when true : execute when false You can also chain the statements (left propagation) foo%100 ? foo%400 ? true : false : false Can be readed like this : foo % 100 is not equal to zero => return false (the last one), foo % 100 is equal to zero => check next condition which is foo% 400 equal to zero. Please check the code for more !

21st May 2017, 12:21 AM
coutable.n
coutable.n - avatar
2 Réponses
+ 14
Thx for sharing, isn't that in the course as well?
23rd May 2017, 7:28 PM
Tashi N
Tashi N - avatar
+ 2
@Tashi N you're welcome ! I can't remember of it in the JavaScript course. I've eard of ternary first in codinGame multiplayer I was always loosing "shortest code" contest haha, now I win sometimes ! Have fun everyone
24th May 2017, 9:44 AM
coutable.n
coutable.n - avatar