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>