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]
Last edited by whiteCollar; October 21st, 2016 at 01:16 PM..
|