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.

CHAPTER 4 ■ CONTROLLING FLOW<br />

120<br />

Figure 4–9. Culling elements containing "" or undefined from looseLeafTea<br />

Replacing Break with Return in a Function<br />

Now just as you can abort a switch with return rather than break whenever the switch appears in a<br />

function, you can abort a loop (while, do while, for, for in) with return rather than break whenever the<br />

loop appears in a function. So, click Clear in both Firebug panels, and let’s rewrite our while loop inside<br />

a function, replacing break with return:<br />

var looseLeafTea = [<br />

"Ghillidary",<br />

"Kenilworth",<br />

"Milima",<br />

"Keemun",<br />

"Boisahabi",<br />

"Manohari",<br />

"Borpatra",<br />

"Lukwah",<br />

"Khongea"<br />

];<br />

function findTea(tea) {<br />

var i = 0;<br />

while (i < looseLeafTea.length) {<br />

if (looseLeafTea[i] === tea) {<br />

return "cheery";

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

Saved successfully!

Ooh no, something went wrong!