Jump to content

How to get a random number created in a textbox or label


pbracing33b
 Share

Recommended Posts

Hello,I have a coding project that I am working on, and I have gotten stumped. Basically win the page loads I want to show random numbers in two textboxes or labels, I would prefer labels. What this is going to be used for is...it is suppose to be for a math learning tool for first, second and third graders. Also another thing that I am suppose to do is have digits based upon their grade level. So for first grade 1+1=2 second grade 2 digits, third 3 digits but can have a four digit answer. Plus it can't have negative numbers, nor have remainders for division. Also I don't want to use buttons if at all possible I want the numbers to load automatically.I know this is a loaded question and I have worked tons on it, for me its trying to get the last few things to work together that's giving me trouble. If you would like to see sections of my code please ask cause it is tons as of right now. Thanks for your help. tongue.gifAlso I have tried several different things, and it seems like everything that I have tried so far doesn't work, so right now I'm not even sure where to begin.

Link to comment
Share on other sites

Thanks foxy mod, but that isn't what I was looking for, here is some of my code so far, sorry it is a bit long but I am not sure what to post. Some of this code right now is useless as well. I always seem to have problems with javascript.

var FKeyPad = document.Keypad;  var txtArea1 = document.txtarea;  var FlagNewNum = false;  //var digit = Math.floor(Math.random()*10)  var a = RandomInt(0,9);  var b = RandomInt(1,9);  //var operation = RandomInt(1,9);  var message = document.getElementById("txtarea");  var add = a + b;  var subtract = a-b;  var divide = a/b;  var multiply = a*b;  var c = Math.floor(Math.random()*100);  var d = Math.floor(Math.random()*100);  var e = Math.floor(Math.random()*1000);  var f = Math.floor(Math.random()*1000);  //testing    function num()  {   if(document.Kepad.number1)   {	RandomInt(0,9);   }  }  function Add()  {   if(document.Keypad.add.checked + document.getElementById('1st grade').checked)   {		document.getElementById('number1').value += a;	document.getElementById('number2').value += b;   }  }  function Subtract()  {   if(document.Keypad.subtract.checked)   {	document.getElementById('number1').value += a;	document.getElementById('number2').value += b;   }  }  function Multiply()  {   if(document.Keypad.multiply.checked)   {	document.getElementById('number1').value += a;	document.getElementById('number2').value += b;   }  }  function Divide()  {   if(document.Keypad.divide.checked)   {	document.getElementById('number1').value += a;	document.getElementById('number2').value += b;   }  }  function firstGrade()  {   if (document.getElementById('1st grade').checked)   {	document.getElementById('number1').value += a;	document.getElementById('number2').value += b;   }  }  function secondGrade()  {   if (document.getElementById('2nd grade').checked)   {	document.getElementById('number1').value += c;	document.getElementById('number2').value += d;   }  }  function checkAns(grade)  {   var answer = document.getElementById(grade+'number1').value + document.getElementById(grade+'number2').value      if (answer == document.getElementById('txtarea'+grade).value)   {   alert("well done!")   }   else   {   alert("try again!")   }  }     document.getElementById("oneA").value=Math.floor(Math.random()*11);   document.getElementById("oneB").value=Math.floor(Math.random()*11);   document.getElementById("twoA").value=Math.floor(Math.random()*101);   document.getElementById("twoB").value=Math.floor(Math.random()*101);   document.getElementById("threeA").value=Math.floor(Math.random()*1001);   document.getElementById("threeB").value=Math.floor(Math.random()*1001);     </script> </head>   <body bgcolor="sky blue">    <p align="center">Welcome to Practice mode of Flash Math!</p> <form name="Keypad" action="" method="post" onsubmit="return false">  <div align="center" width="140" height="30">	<tr><input type="image" id="1" src="Button1.gif" alt="Calculator Button #1" onclick="document.getElementById('txtarea').value += '1'"/>  <input type="image" id="2" src="Button2.gif" alt="Calculator Button #2" onclick="document.getElementById('txtarea').value += '2'"/>  <input type="image" id="3" src="Button3.gif" alt="Calculator Button #3"onclick="document.getElementById('txtarea').value += '3'"/><div>   <div>	<input type="image" id="4" src="Button4.gif" alt="Calculator Button #4"onclick="document.getElementById('txtarea').value += '4'"/>  <input type="image" id="5" src="Button5.gif" alt="Calculator Button #5" onclick="document.getElementById('txtarea').value += '5'"/>  <input type="image" id="6" src="Button6.gif" alt="Calculator Button #6" onclick="document.getElementById('txtarea').value += '6'"/>   <div>	<input type="image" id="7" src="Button7.gif" alt="Calculator Button #7" onclick="document.getElementById('txtarea').value += '7'"/>  <input type="image" id="8" src="Button8.gif" alt="Calculator Button #8" onclick="document.getElementById('txtarea').value += '8'"/>  <input type="image" id="9" src="Button9.gif" alt="Calculator Button #9" onclick="document.getElementById('txtarea').value += '9'"/>   <div>	<input type="image" id="0" src="Button0.gif" alt="Calculator Button #0" onclick="document.getElementById('txtarea').value += '0'"/>    <div>	<p align="center"><label>Your current Math Problem:</label><br><input type="text" name="number1" id="number1"readonly="readonly" /> <br>	<!--<textarea rows="4" cols="8" readonly="readonly"></textarea>--><input type="text" name="number2" id="number2"readonly="readonly"/>	<br><label>Please enter your answer here:</label><br><input type="text" name="txtareaMath" id="txtarea" size="25"></p>   <div>	<p align="center"><label>Please select an operation:</label><input type="checkbox" name="add" value="add" id="add" onclick="Add()"/>Add<input type="checkbox" name="subtract" value="subtract" id="subtract" onclick="Subtract()"/>Subtract<input type="checkbox" name="multiply" value="multiply" id="multiply" onclick="Multiply()"/>Multiply<input type="checkbox" name="divide" value="divide" id="divide" onclick="Divide()"/>Divide</p>   <div>	<label>Please select a grade level:</label>	<input type="radio" name="grade" id="1st grade" value="First Grade" checked="checked"onclick="Add()"/>First Grade <input type="radio" name="grade" id="2nd grade" value="Second Grade"/>Second Grade <input type="radio" name="grade" id="3rd grade" value="Third Grade"/>Third Grade   <div>	<label>Please select the number of problems:</label>	<input type="radio" name="amount" id="5" value="five" checked="checked"/>5<input type="radio" name="amount" id="10" value="ten"/>10<input type="radio" name="amount" id="15" value="fifteen"/>15<input type="radio" name="amount" id="20" value="twenty"/>20<input type="radio" name="amount" id="25" value="twenty-five"/>25<input type="radio" name="amount" id="30" value="thirty"/>30   <div>	<input type="button" name="submit" id="submit" value="submit" onclick="checkAns('txtarea')"/><input type="reset"/>   <div>	first grade:<input type="text" id="oneA"><input type="text" id="oneB">answer:<input type="text" id="ansone"><input type="button" value="check" onclick="checkAns('one')"/><br>	second grade:<input type="text" id="twoA"><input type="text" id="twoB">answer:<input type="text" id="anstwo"><input type="button" value="check" onclick="checkAns('two')"/><br>	third grade:<input type="text" id="threeA"><input type="text" id="threeB">answer:<input type="text" id="ansthree"><input type="button" value="check" onclick="checkAns('three')"/><br>    </form>   </body>  

Link to comment
Share on other sites

There are some things that aren't right in your script. And your HTML has a lot of problems in it. You should check the W3C validator. Since this is pure Javascript, there's no need for forms at all. Forms are only for server-side communication. Don't count on elements showing up as properties of the document object. You should access them with getElementById() or getElementsByTagName() as you did in other parts of the script. A lot of your functions don't seem to be used, and some functions that are being used don't seem to be defined anywhere. Element IDs can't have spaces in them and must start with a non-numeric character, so this code probably won't work: document.getElementById('1st grade').To put a random number inside an element, just give the element an ID, generate the number and put it in the element.

<span id="random"></span> document.getElementById("random").innerHTML = Math.floor(Math.random() * 899) + 100

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...