Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Using a while loop

Code Wizard, 12-30-2006

Using a while loop to take user input from an alert box (Credit: apress.com)

<script type="text/javascript">
var userNumbers = new Array();
var userInput = 0;
var arrayIndex = 0;
var message = '';
var total = 0;
//loop for as long as the user doesn't input 99
while (userInput != 99) {
userInput = prompt("Enter a number, or 99 to exit","99");
userNumbers[arrayIndex] = userInput;
arrayIndex++;
}
message+='You entered the following:n';
for (var i=0;i<arrayIndex-1;i++){
message+=userNumbers[i]+'n';
total+=Number(userNumbers[i]);
}
message+='Total: ' + total + 'n';
alert(message);
</script>
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions