Adding an integer using Math.random

Discussion in 'JavaScript' started by clarky101, Dec 6, 2007.

  1. #1
    hi i need to insert an integer using math.random into the following code and name it aNumber which produces an integer in the range 1 to 12 but am not sure on how i go about completing this. any advice would be much appreciated.

           <html>
            <head>
    
            <script language=javascript type="text/javascript">
            document.writeln("<h1>Learning to Multiply Web Site</h1>");
    
            function checkAnswer() {
            value1 = document.forms[0].value1.value; // user dom to set value1 variable to value of 
    
    first input
            value2 = document.forms[0].value2.value; // user dom to set value2 variable to value of 
    
    second input 
    
            answer = value1 * value2; // times 2 values
    
            document.forms[0].result.value = answer; //set result field equal to answer variable.
            }
            </script>
    
    
            </head>
            <body>
    
            <form name="myForm" id="myForm" action="">
    
            <table border = "1">
    
            <tr>
             <td> What is</td>
             <td><input name="value1" type="text" /></td>
            </tr>
            <tr>
             <td>Multiplyed By</td>
             <td><input name="value2" type="text" /></td>
            </tr>
            <tr>
             <td>The answer is</td>
             <td><input name = "result" type = "text" /></td>
            </tr>
             <td><input type= "button" value = "Check My Answer" onclick="checkAnswer()" /></td>
            </tr>
    
            </table>
            </form>
    
            </body>
            </html>
    PHP:
     
    clarky101, Dec 6, 2007 IP
  2. James McMurray

    James McMurray Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var aNumber=Math.floor(Math.random()*13);

    Very often googling for javascript plus whatever you're wanting to do will point you in the right direction. Adding "tutorial" often helps. That link was found via "javascript random number tutorial".
     
    James McMurray, Dec 6, 2007 IP
  3. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function init(){
    		
    		document.forms[0]['aNumber'].value = Number(parseInt(Math.random() * 12) + 1);
    	}
    
    	onload=init;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color: #fffacd; margin-top: 60px;}
    	 form {width: 620px; margin: auto; font-family: arial; font-size: 12pt;}	
    	 fieldset {background-color: #eee8aa; border: 1px solid #e6b280; padding-left: 8px; padding-bottom: 8px;} 
    	 legend {font-family: arial; font-size: 14pt; color: #000000; background-color: transparent; padding-top: 3px; padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
    	.input_field {text-align: right;}
    	.formBtn {background-color: #ffffff; border: solid 1px #000000; font-family: arial; font-size: 10pt; font-weight: bold; cursor: pointer; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
    	
    </style>
    </head>
    	<body>
    		<form method="post" action="">
    		   <fieldset>
    			<legend>Form</legend>
    				<input type="text" name="aNumber" size="5" class="input_field" readonly>
    				<br>				
    				<input type="button" value="New Random" class="formBtn" onclick="init()">
    				<input type="submit" name="submit" value="Submit" class='formBtn'>
    		   </fieldset>
    		</form>
    	</body>
    </html>
    
    Code (markup):
     
    Mike H., Dec 6, 2007 IP