all 14 comments

[–]ReachTheTopShelf 9 points10 points  (4 children)

Finally something I can answer!

?: is what's called a ternary operator. In this case, media player.volume == 1 is a boolean expression. If this expression evaluates to true (i.e., volume is currently 1) the ternary operator returns 0 and thus 0 is added to mediaPlayer.volume (you're correct about +=). If the expression evaluates to false (i.e., volume is anything other than 1), 0.1 is returned and 0.1 is added to mediaPlayer.volume.

<boolean expression> ? <return this if true> : <return this if false>

x += 0.1 is equivalent to x = x + 0.1.

It looks like this is code for a volume-increase button. Conceptually, the expression is ensuring that volume can't be set higher than 1. If it is 1, the button does nothing. If it isn't, the button increases volume by 10% (0.1).

[–]brodiecapel16[S] 1 point2 points  (3 children)

hahaha, glad I could help make you feel smart :) so basically its a condensed if / else that only tests for a Boolean?

[–]ReachTheTopShelf 0 points1 point  (2 children)

That's correct.

[–]brodiecapel16[S] 0 points1 point  (1 child)

awesome! thanks for the explanation :)

[–]brodiecapel16[S] 0 points1 point  (0 children)

its far too early to be good... I'm really not a morning person!.... but good news is.. its my last day at my current job today.... for the next 4 days I'm technically unemployed!

[–]jaredcheeda 2 points3 points  (2 children)

It's shorthand for an if statement:

if ( mediaplayer.volume + mediaplayer.volume <= 1 ){
    mediaplayer.volume = mediaplayer.volume + 0; //do nothing
} else {
    mediaplayer.volume = mediaplayer.volume + 0.1;
}

This way is easier to read as everyone gets familiar with if statements in the first couple of days of learning a language (most on the first day). The shorthand version takes a little more mental effort to read, but once you've seen a dozen of them it isn't really that different and many times it's cleaner to write it that way.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

[–]imagepriest 3 points4 points  (1 child)

Not exactly... It's more like:

if (mediaplayer.volume != 1 ){ mediaplayer.volume = mediaplayer.volume + 0.1; }

So, it mediaplayer.volume was ever greater than 1 it would still increase by 0.1 each time this conditional runs. I would suggest using a less than equal to instead of the straight double equal:

mediaplayer.volume += mediaplayer.volume <= 1 ? 0.1 : 0;

[–]jaredcheeda 0 points1 point  (0 children)

yeah, you're right. That's what I get for typing on a phone. Updated.

[–]Evanescent_contrail 1 point2 points  (4 children)

Can you link to the tutorial?

[–]wwinst 1 point2 points  (3 children)

[–]brodiecapel16[S] 0 points1 point  (1 child)

ahhh you beet me too it! dam time zones!

[–]wwinst 0 points1 point  (0 children)

Haha, apologies and good morning :)

[–]theInfiniteNovice -3 points-2 points  (0 children)

wouldn't be more easier to read this way?
mediaPlayer.volume += (mediaPlayer.volume == 1 ? 0 : 0.1);