Wrox Programmer Forums
BOOK: Beginning JavaScript 4th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 4th Edition by Paul Wilton, Jeremy McPeak; ISBN: 978-0-470-52593-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 4th Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 21st, 2016, 01:04 PM
Registered User
Join Date: Oct 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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;

Last edited by whiteCollar; October 21st, 2016 at 01:16 PM..

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4 --> Chapter 5 Continuity?? theboffin BOOK: Professional ASP.NET MVC 4 4 July 8th, 2013 03:36 AM
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.