www.webdeveloper.com
Results 1 to 2 of 2

Thread: Prompt user for number

  1. #1
    Join Date
    Dec 2013
    Posts
    7

    Prompt user for number

    How do I modify this code to take user input from prompt to act as a variable in order to write all numbers leading up to user input. EX user enters 5, 1,2,3,4 would be displayed - thanks in advance

    <code>
    <html>
    <body>
    <script>
    var i= 1;
    while (i <= 5){
    document.write (i + "");
    i++;
    }
    </script>
    </body>
    </html>
    </code>

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="utf-8" />
    	<title>Prompt user for number</title>
    </head>
    <body>
    	<script type="text/javascript">
    
    		var
    			i = 1,
    			n = parseInt(prompt('Please enter a number', 5), 10);
    
    		while (i <= n){
    			document.write((i > 1 ? ',' : '') + i);
    			i++;
    		}
    
    	</script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles