p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning JavaScript 4th Edition (http://p2p.wrox.com/book-beginning-javascript-4th-edition-529/)
-   -   Chapter 6: Example 5 (http://p2p.wrox.com/book-beginning-javascript-4th-edition/99230-chapter-6-example-5-a.html)

whiteCollar October 21st, 2016 02:04 PM

Chapter 6: Example 5
 
How is the "while" condition working in this code? I tried to see the value of "that.src.indexOf('usa.gif')" by printing it out on the page. The image I click displays 42 as its value and the other one displays -1. But still I couldn't understand it. Can somebody please explain the logic here and also why is the image which is being clicked returns 42 as its value? Thanks!

CODE]<script type="text/javascript">
var myImages = new Array("usa.gif", "canada.gif", "jamaica.gif", "mexico.gif");
function changeImg(that) {
var newImgNumber = Math.round(Math.random() * 3);
while(that.src.indexOf(myImages[newImgNumber]) != -1)
{
newImgNumber = Math.round(Math.random() * 3);
}
that.src = myImages[newImgNumber];
return false;
}
</script>[/code]


All times are GMT -4. The time now is 12:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.