Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 4th Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 10th, 2014, 12:45 AM
les les 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: Dec 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5 -Counting Occurrences of sub-strings

Wondering if somebody can explain how the line 'while (foundAtPosition != -1)' in the code below, works.

I understand that it is checking for a condition but don't understand the significance of "not equal to -1" as an escape condition. It has to be something straightforward but I'm beating my head!

Code:
var myString= "Welcome to Wrox books.";
myString= myString + "The Wrox website is www.wrox.com .";
myString =myString + "Visit the Wrox website today and thanks for buying Wrox";


var foundAtPosition=0;
var wroxCount=0;
// DON'T UNDERSTAND HOW THIS NEXT LINE WORKS
while (foundAtPosition != -1)
{
	foundAtPosition = myString.indexOf("Wrox",foundAtPosition);
	if (foundAtPosition != -1)
	
	{
		wroxCount++;
		foundAtPosition++;
	}
}

document.write("There are  " + wroxCount + " occurrences of the work Wrox.");	

</script>

</body>

</html>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 code modification. Counting the number of times a certain string occurs. scolty BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 3 April 28th, 2011 09:29 AM
Chapter 7: Counting Characters/Words alanbenjy BOOK: Beginning Microsoft Visual Basic 2010 0 April 13th, 2011 10:36 AM
XSLT Code for Multiple occurrences vijayp2p XSLT 1 May 9th, 2006 12:23 PM
xslt with multiple occurrences in a record Olaf_l XSLT 2 April 7th, 2005 08:02 AM
Distinct Column Occurrences jemacc SQL Server 2000 2 October 25th, 2004 05:20 AM



All times are GMT -4. The time now is 09:52 AM.


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