23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4 ■ CONTROLLING FLOW<br />

144<br />

var d = topTwenty[p1] - topTwenty[p2];<br />

if (d !== 0) {<br />

return d;<br />

} else {<br />

return (p2 < p1) ? -1 : 1;<br />

}<br />

});<br />

var i = rocketRichard.length - 1;<br />

do {<br />

note = note + rocketRichard[i] + ": " + topTwenty[rocketRichard[i]] + ", ";<br />

} while (i --);<br />

note.slice(0, -2);<br />

// "Crosby: 49, Ovechkin: 48, Stamkos: 48, Marleau: 43"<br />

So, there it is. We’re done exploring conditionals and loops. By the way, Sidney Crosby and Steven<br />

Stamkos finished in a tie for most goals in the NHL with 51 apiece. They’ll share the Rocket Richard<br />

trophy. Note to NHL: I’d suggest fewest empty net goals being the tie-breaker for the Rocket Richard in<br />

the future. Crosby had just one empty net goal this year, while Stamkos had five, including number 51!<br />

Summary<br />

In this chapter, you learned how to control flow with if and switch conditional statements and with<br />

while, do while, for, and for in looping statements. You also learned to make decisions, as JavaScript<br />

does, with boolean expressions. true is a green light to do something, and false is a red light not to.<br />

In the next chapter, we will delve more deeply into objects and arrays, folder-like datatypes for<br />

organizing data. Take a well-deserved break, and I’ll see you there!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!