View Single Post
  #1 (permalink)  
Old October 21st, 2016, 02:04 PM
whiteCollar whiteCollar is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2016
Location: New Delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to whiteCollar
Post 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 02:16 PM.
Reply With Quote