Jump to content

Arithmetic Practice, JavaScript + HTML DOM Code

- - - - -

  • Please log in to reply
No replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I thought maybe the SAT would have those arithmetic questions the PSAT had, and I thought maybe people would need practice with that, so I decided to write this program just for that. It includes multiplication, division, addition, and subtraction.

The questions look something like "82 + 97" , or "7 * 24" , etc. The numbers in the question don't get larger than 2 digits each, but one can change that on lines 88 and 89.

The right answer is rounded to 3 decimal places, but that's modifiable too, by setting the round_places variable on line 11.


Here's the code:
<html> 

	<head> 

		<title> JavaScript required... </title> 

		<script type="text/javascript"> 

			document.title= "Arithmetic"; 

		</script> 

	</head> 

	<body> 

		<script type="text/javascript"> 

			//<!-- 

			var round_places= 3; 

			var t1, t2, t3, t4, t5, t6, t7, t8; 

			var timer_element; 

			var timer_fxn; 

			var timer_int; 

			var time1; 

			var fxn= [ 

				function (a, b){ 

					if (!a && !b) return "*"; 

					else return parseFloat (a) * parseFloat (b); 

				}, function (a, b){ 

					if (!a && !b) return "/"; 

					else return parseFloat (a) / parseFloat (b); 

				}, function (a, b){ 

					if (!a && !b) return "+"; 

					else return parseFloat (a) + parseFloat (b); 

				}, function (a, b){ 

					if (!a && !b) return "-"; 

					else return parseFloat (a) - parseFloat (b); 

				} 

				]; 

			var q; 

			var a; 

			var one; 

			var two; 

			var answer; 

			var f_index; 

			var feedback; 

			var input_div; 

			var completed= 0; 

			var completed_element; 

			document.body.appendChild (t1= document.createElement ("h1")); 

				t1.appendChild (document.createTextNode ("Arithmetic")); 

			t1= document.createElement ("div"); 

			t1.appendChild (feedback= document.createElement ("div")); 

			t1.appendChild (input_div= document.createElement ("div")); 

			t1.appendChild (t2= document.createElement ("button")); 

				t2.appendChild (document.createTextNode ("Start")); 

				t2.onclick= function (){ 

					time1= new Date (); 

					t3= document.createElement ("div"); 

						t3.appendChild (document.createTextNode ("Total Time: ")); 

						t3.appendChild (timer_element= document.createElement ("span")); 

							timer_fxn= function (){ 

								var time= new Date () - time1; 

								var ts= ""; 

								var t2; 

								var cn; 

								t2= "" + (cn= parseInt (time / 60 / 60 / 1000)); 

									while (t2.length < 2) t2= "0" + t2; 

									ts += t2; 

									ts += ":"; 

								t2= "" + (cn= parseInt (time / 60 / 1000 - cn * 60)); 

									while (t2.length < 2) t2= "0" + t2; 

									ts += t2; 

									ts += ":"; 

								t2= "" + (cn= parseInt (time / 1000) - cn * 60); 

									while (t2.length < 2) t2= "0" + t2; 

									ts += t2; 

									ts += "."; 

								t2= "" + (cn= time - cn * 1000); 

									while (t2.length < 3) t2= "0" + t2; 

									ts += t2; 

								while (timer_element.childNodes[0]) timer_element.removeChild (timer_element.childNodes[0]); 

								timer_element.appendChild (document.createTextNode (ts)); 

							}; 

						timer_int= setInterval ("timer_fxn ();", 27); 

					t1.appendChild (document.createElement ("br")); 

					t1.appendChild (t3); 

					t4= document.createElement ("div"); 

						t4.appendChild (document.createTextNode ("Completed: ")); 

						t4.appendChild (completed_element= document.createElement ("span")); 

					t1.appendChild (t4); 

					while (t2.childNodes[0]) t2.removeChild (t2.childNodes[0]); 

					t2.onclick= function (mode){ 

						while (feedback.childNodes[0]) feedback.removeChild (feedback.childNodes[0]); 

						if (mode == "init" || a.value == "" + answer){ 

							one= Math.floor (Math.random () * 100); 

							two= Math.floor (Math.random () * 100); 

							f_index= Math.floor (Math.random () * fxn.length); 

							while (q.childNodes[0]) q.removeChild (q.childNodes[0]); 

							q.appendChild (document.createTextNode (one + " " + fxn[f_index] () + " " + two)); 

							a.value= ""; 

							a.focus (); 

							answer= fxn[f_index] (one, two); 

							if (!isNaN (parseFloat (answer))){ 

								answer= Math.round (parseFloat (answer) * Math.pow (10, round_places)) / Math.pow (10, round_places); 

							} 

							if (mode == "init"){ 

								

							} else { 

								feedback.style.color= "#00FF00"; 

								feedback.appendChild (document.createTextNode ("Correct! You may proceed on to the next question.")); 

								while (completed_element.childNodes[0]) completed_element.removeChild (completed_element.childNodes[0]); 

								completed_element.appendChild (document.createTextNode (completed= completed + 1)); 

							} 

						} else { 

							feedback.style.color= "#FF0000"; 

							feedback.appendChild (document.createTextNode ("Wrong. Please answer again, or ")); 

							feedback.appendChild (t5= document.createElement ("a")); 

								t5.href= "javascript:;"; 

								t5.onclick= function (){ 

									t2.onclick ("init"); 

								}; 

								t5.appendChild (document.createTextNode ("click here")); 

								t5= 0; 

							feedback.appendChild (document.createTextNode (" to skip this question. ")); 

						} 

					}; 

					t2.appendChild (document.createTextNode ("Respond")); 

					input_div.appendChild (q= document.createElement ("question")); 

					input_div.appendChild (document.createElement ("br")); 

					input_div.appendChild (a= document.createElement ("input")); 

						a.type= "text"; 

					t2.onclick ("init"); 

				}; 

			document.body.appendChild (t1); 

			// --> 

		</script> 

		<noscript> 

			Your browser needs to use JavaScript in order to display this content. 

		</noscript> 

	</body> 

</html> 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users