Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While Loop

Status
Not open for further replies.

mmt4331

Programmer
Dec 18, 2000
125
0
0
US

The code below works to a degree. I ask the user to input a number less than 10...if he puts in 10 or greater, I shoot him an alert saying his value is too high. Then the program ends. How can I put where the user, if he puts it greater than ten, go back to the original prompt to ask him again to input a value. I'm trying to learn javascript on my own, and this is puzzling me a little. I'm thinking a while loop may work, but wouldn't quite know how to do it. Any thoughts?

The code is below:

<html>
<title>Practice</title>
<head>
<script language=&quot;javascript&quot;>

var lowConstant = 10;
var numCounter = prompt(&quot;Please enter number&quot;,&quot;Number&quot;);
var StepUpCounter = parseInt(numCounter);

if (StepUpCounter >= lowConstant)
{
alert(&quot;Please enter number less than 10&quot;,&quot;XNumber&quot;)
}
else
{
document.writeln(smallLoop())
}


function smallLoop()

{StepUpCounter = StepUpCounter + lowConstant;
return StepUpCounter;
};

</script>
</head>
</html>

 
When you return a value from a function it must go somewhere ie. into a variable so write-


else{
var n = smallLoop();

document.writeln(n);
}
 
I did that, but that still doesn't solve my problem. How do I code it where it goes back to the same prompt if the user puts a value equal or greater than 10? Thank you.
 
function F(){

while(StepUpCounter >= lowConstant){
window.prompt(&quot;your message&quot;);
}
document.write(smallLoop());

}

~mgb
 
function F(){

while(StepUpCounter >= lowConstant){
numCounter = window.prompt(&quot;your message&quot;);
}
document.write(smallLoop());

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top