Maths Game

Generating random Maths questions

Page 1 of 1

9 Replies - 2691 Views - Last Post: 22 March 2008 - 11:30 AM

#1 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Maths Game

Post icon  Posted 13 March 2008 - 01:04 PM

Hello ,i was wondering if anyone can help me with this tonight as it is an assignment that is due in tomorrow.The code i have written below generates single random numbers in a prompt box -what it is meant to do is show the 10 prompt boxes with a maths question in each eg 2 + 3 or 5 x 8 etc. The questions have to be generated randomly -which is where i am stuck as i can get a single random number in each box but not an actual calculation -any ideas would be very very grateful received


<html>
   <head>
   <title>Tutorial7.html</title>
   
   <link  rel="stylesheet" type ="text/css" href="style.css"\>
       
  <h1>Maths Game</h1>      
  </head>



  <script language="javascript">[size=7]
    

   
   var  result1 =prompt(Math.floor(Math.random()*101));
   var  result2 =prompt(Math.floor(Math.random()*101)); 
   var  result3 =prompt(Math.floor(Math.random()*101));
   var  result4 =prompt(Math.floor(Math.random()*101)); 
   var  result5 =prompt(Math.floor(Math.random()*101)); 
   var  result6 =prompt(Math.floor(Math.random()*101)); 
   var  result7 =prompt(Math.floor(Math.random()*101)); 
   var  result8 =prompt(Math.floor(Math.random()*101)); 
   var  result9 =prompt(Math.floor(Math.random()*101)); 
   var  result10=prompt(Math.floor(Math.random()*101)); 
   
 
    if( result1 ==" ")
    { 
     document.write("+...correct <br>");
    }
   else 
    {
     document.write(result1 + " is incorrect.The correct answer is:  <br>");
    }
 
    if (result2 == " ")
    {
     document.write( "x...correct. <br>");
    }
    else
    {
     document.write(result2 +" is incorrect.The correct answer is:  <br>");
    }
    if (result3 == " ")
    {
     document.write( " -...correct. <br>");
    }
    else
     {
     document.write(result3 +" is incorrect.The correct answer is:  <br>");
     }
     if (result4 == " ")
     {
     document.write( "x...correct. <br>");
     }
    else
     {
      document.write(result4 +" is incorrect.The correct answer is:  <br>");
     }
     if (result5 == " ")
     {
      document.write( "-...correct. <br>");
     }
      else
     {
      document.write(result5 +" is incorrect.The correct answer is:  <br> ");
     }
    if (result6 == " ")
     { 
      document.write( "-...correct. <br>");
     }
     else
     {
      document.write(result6 +" is incorrect.The correct answer is:  <br>");
      }
     if (result7 == " ")
      {
      document.write( "x...correct. <br>");
      }
     else
      {
      document.write( result7 +" is incorrect.The correct answer is: <br>");
      } 
     if (result8 == " ")
      {
      document.write( "+...correct. <br>");
      }
     else
      {
      document.write (result8 +" is incorrect.The correct answer is:  <br>");
      }
     if (result9 == " ")
      {
      document.write( "-...correct. <br>");
      }
      else
      {
      document.write (result9 +" is incorrect.The correct answer is:  <br>");
      }
     if (result10 == " ")
      { 
      document.write ( "x...correct. <br>");
      }
      else
      {
      document.write (result10 +" is incorrect.The correct answer is: <br>");
      }
  </script>






</body>

</html>

Please use code tags when posting your code as it makes it easier to read. Code blocks are used like :code:

Thanks :)

This post has been edited by PsychoCoder: 13 March 2008 - 02:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Maths Game

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Maths Game

Posted 13 March 2008 - 01:55 PM

Hello
you can make it shorter than what you did by using (for) loop
and here is what you have to do
<script language="javascript">
answers=""
for (i=0;i<10;i++){
a=Math.floor(Math.random()*100)+1
b=Math.floor(Math.random()*100)+1
oper=new Array(" + "," - "," * "," / ");
operrand=Math.floor(Math.random()*4);
formula=String(a+oper[operrand]+B)/>
r=prompt("Quest # "+Number(i+1)+" : "+formula,"");
ans=eval(formula)
if (r!=ans){
	answers+="Result "+Number(i+1)+" is incorrect.The answer is: "+ans+"<br>"
	}
}
document.write(answers);
</script>


Good Luck
Was This Post Helpful? 0
  • +
  • -

#3 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Maths Game

Posted 13 March 2008 - 02:32 PM

Hi , i am afraid that code didn't work, the page was just blank, the questions have to be inside a prompt box-with a prompt for the answer from the user.which is then recorded and displayed at the end of all the questions.

It doesn't need to be complex code it doesn't have to use a loop or anything either-i am not bothered as to length of the code just that it works!:)
thanks anyway
Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Maths Game

Posted 13 March 2008 - 02:46 PM

Hi Tara,

The code ahmad was right, but the forum ate part of line 8 (b followed by a closing bracket makes the sunglasses emote). This version should work:

<script language="javascript">
answers=""
for (i=0;i<10;i++){
a=Math.floor(Math.random()*100)+1
b=Math.floor(Math.random()*100)+1
oper=new Array(" + "," - "," * "," / ");
operrand=Math.floor(Math.random()*4);
formula=String(a+oper[operrand]+b+"")
r=prompt("Quest # "+Number(i+1)+" : "+formula,"");
ans=eval(formula)
if (r!=ans){
	answers+="Result "+Number(i+1)+" is incorrect.The answer is: "+ans+"<br>"
	}
}
document.write(answers);
</script>
</body>


Was This Post Helpful? 0
  • +
  • -

#5 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Maths Game

Posted 13 March 2008 - 03:51 PM

Thanks thehat, I didn't notice that

This post has been edited by ahmad_511: 13 March 2008 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Maths Game

Posted 13 March 2008 - 04:11 PM

Thanks Ahmed , and yes i did get it to work but much as i would love to use it but i am only a beginner so i don't think it will wash with my tutor tomorrow:)But thanks anyway
Was This Post Helpful? 0
  • +
  • -

#7 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Maths Game

Posted 13 March 2008 - 05:24 PM

Is there a way i could adapt my original code to work like yours does Ahmed? My tutor just won't believe i have done that otherwise:)
Was This Post Helpful? 0
  • +
  • -

#8 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Maths Game

Posted 13 March 2008 - 05:57 PM

hi again
<script>
oper=new Array("+","-","*","/")
// <<start>> 
a=Math.floor(Math.random()*101);  
b=Math.floor(Math.random()*101);  
o=oper[Math.floor(Math.random()*4)]; 
answer1=eval(a+o+b)
result1=prompt(a+o+b,"");

// repeat from <<start>> as many as you want 
// make sure that the next variables will be result2,result3,--- and answer2,answer3,---

// comparing the results with the answers 
if (result1!=answer1)document.write(result1 +" is incorrect.The correct answer is: "+answer1+"<br>")
//repeat the previouse line to match all results and answers you added before
</script>



I hope this helps

Edit: the forum did it again , now it ate the HTML break tag so the last line will become
if (result1!=answer1)document.write(result1 +" is incorrect.The correct answer is: "+answer1+"<br>")

This post has been edited by ahmad_511: 13 March 2008 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 Tara200  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 85
  • Joined: 13-October 07

Re: Maths Game

Posted 21 March 2008 - 11:03 AM

Yes thankyou very much!!!
Was This Post Helpful? 0
  • +
  • -

#10 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Maths Game

Posted 22 March 2008 - 11:30 AM

You're welcome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1