Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 4th Edition
|
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 December 10th, 2014, 12:45 AM
les les is offline
Registered User
 
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>





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 08:29 AM
Chapter 7: Counting Characters/Words alanbenjy BOOK: Beginning Microsoft Visual Basic 2010 0 April 13th, 2011 09:36 AM
XSLT Code for Multiple occurrences vijayp2p XSLT 1 May 9th, 2006 11:23 AM
xslt with multiple occurrences in a record Olaf_l XSLT 2 April 7th, 2005 07:02 AM
Distinct Column Occurrences jemacc SQL Server 2000 2 October 25th, 2004 04:20 AM





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