...

View Full Version : Resolved While Loops



galaxiesanddust
10-10-2012, 07:33 PM
I have a code that I can't seem to get to work and it's been driving me nuts for hours.

I have to use a While Loop to prompt the user for the name of their favorite team. Use a while loop to display a cheer three times. If the user entered "Braves", the program would output:
Go Braves!
Go Braves!
Go Braves!

the example code the book gives me is

while ( index < 3) {
document.write("Beetlejuice ");
index = index + 1;

}

The prompt works fine, but I can't get it to display properly. Any help would aid in me not smashing my head on the keyboard anymore.

Thank you!

WolfShade
10-10-2012, 07:41 PM
Did you set index to equal 1 before the loop?

Don't forget to reset to 0 after the loop.

galaxiesanddust
10-10-2012, 07:52 PM
I did set index to 0 before the loop. This is the code I have so far. I'm fairly new to all this.


<html>
<body>
<script type="text/javascript">

// Program name: cheer.html
// Purpose:
// Author:
// Date last modified:

// Declare Variables and Constants
var teamName;
var index = 0;

// Prompt user for the name of their favorite team
teamName = prompt("Enter your favorite team");
teamName = parseFloat(teamName);

// Display
index = 0;
while ( index < 3); {
document.write( "teamName" );
index = index + 1;

}

</script>
</body>
</html>

WolfShade
10-10-2012, 07:54 PM
parseFloat is for integers. What are you trying to do to the string?

Also.. in this line, remove the quotes.
document.write( "teamName" );

Wuteverx1972
10-10-2012, 07:59 PM
Try using a for loop:


$a = "Go ";
$a .= "[Team]!";

$i = $a;
for ($n=0; $n<3; $n++) {
$output = "<br/>".$i++ . "\n";

echo $output;
}

Output dispaly:


Go [Team]!
Go [Team]!
Go [Team]!

WolfShade
10-10-2012, 08:01 PM
OP isn't using PHP, AFAIK.

galaxiesanddust
10-10-2012, 08:02 PM
I removed the quotes and changed parseFloat. I'm just trying to have whatever the user enters in prompt at the beginning to appear three times. Is there a way I need to close the loop or something that I am overlooking? Right now the page just acts like it's continuously loading.

WolfShade
10-10-2012, 08:07 PM
There's also a semicolon where there shouldn't be. (while ( index < 3); {)

Try this:

<html>
<body>
<script type="text/javascript">

// Program name: cheer.html
// Purpose:
// Author:
// Date last modified:

// Declare Variables and Constants
var teamName;
var index = 0;

// Prompt user for the name of their favorite team
teamName = prompt("Enter your favorite team");

// Display
for ( index=0; index < 3; index++) {
document.writeln( teamName + " " );

}

</script>
</body>
</html>

galaxiesanddust
10-10-2012, 08:14 PM
That worked. Thank you so much for the help!

WolfShade
10-10-2012, 08:27 PM
np.. glad I could help.. please edit your original post and change the header to RESOLVED.

Philip M
10-10-2012, 09:44 PM
The guy asked for a while loop.


<html>
<head>
</head>

<body>
<script type="text/javascript">

// Program name: cheer.html
// Purpose:
// Author:
// Date last modified:

// Declare Variables and Constants
var teamName;
var index = 0;
var Jmessage = "";
var Hmessage = "";


// Prompt user for the name of their favorite team
teamName = prompt("Enter your favorite team", "");

// Display
while ( index < 3) {
Jmessage += teamName + "\n"; // Note \n is newline in Javascript
Hmessage += teamName + "<br>"; // But <br> is newline in HTML
index ++;
}

alert (Jmessage);
document.write(Hmessage); // document.write() creates a new page

</script>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum