misheck
12-08-2008, 06:00 PM
I am studying javascript and I have stumbled accross an exercise section where the script is not working. It is suppose to write the number of occurrences of the word Wrox. <script language="JavaScript" type="text/javascript">
var myString = "Welcome to Wrox books. ";
myString = myString + "The Wrox website is www.wrox.com. ";
myString = myString + "Visit the Wrox website today. Thanks for buying Wrox";
var foundAtPosition = 0;
var wroxCount = 0;
while ( foundAtPosition != -1)
{
foundAtPostion = myString.indexOf("Wrox",foundAtPosition);
if (foundAtPosition != -1)
{
wroxCount++;
foundAtPosition++;
}
}
document.write("There are " + wroxCount + " occurrences of the word Wrox");
</script> The last foundAtPosition++; is meant to stop the script from running infinitely but it seems to be running continously and not writing anything to the screen.
var myString = "Welcome to Wrox books. ";
myString = myString + "The Wrox website is www.wrox.com. ";
myString = myString + "Visit the Wrox website today. Thanks for buying Wrox";
var foundAtPosition = 0;
var wroxCount = 0;
while ( foundAtPosition != -1)
{
foundAtPostion = myString.indexOf("Wrox",foundAtPosition);
if (foundAtPosition != -1)
{
wroxCount++;
foundAtPosition++;
}
}
document.write("There are " + wroxCount + " occurrences of the word Wrox");
</script> The last foundAtPosition++; is meant to stop the script from running infinitely but it seems to be running continously and not writing anything to the screen.