23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

"Semin": 39,<br />

"Parise": 37,<br />

"Burrows": 35,<br />

"Kopitar": 34,<br />

"Ryan": 34,<br />

"Carter": 33,<br />

"Nash": 33,<br />

"Iginla": 32,<br />

"Penner": 32,<br />

"Backstrom": 31,<br />

"Hornqvist": 30,<br />

"Jokinen": 30,<br />

"Kane": 30<br />

}<br />

var rocketRichard = ["Ovechkin", "Crosby", "Marleau", "Stamkos"], note = "";<br />

rocketRichard.sort(function(p1, p2) {<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 />

for (var i = rocketRichard.length; i --; ) {<br />

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

}<br />

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

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

CHAPTER 4 ■ CONTROLLING FLOW<br />

Note that in i --, the -- operator is in the post-decrement position. Why does that matter? It<br />

matters for a couple of reasons. For one thing, if you wrote -- i instead of i --, JavaScript would never<br />

query the fourth element in rocketRichard. For another, if rocketRichard were empty, which is to say its<br />

length was 0, then our for loop would never stop iterating. So, be sure that -- is in the post-decrement<br />

position!<br />

141

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

Saved successfully!

Ooh no, something went wrong!