# Maths Game

Page 1 of 1

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

### #1 Tara200

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

# Maths Game

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>
<title>Tutorial7.html</title>

<h1>Maths Game</h1>

```  <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

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

• MSX

Reputation: 131
• 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">
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){
}
}
</script>

```

Good Luck

### #3 Tara200

Reputation: -10
• 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

### #4 thehat

• awake ? web();

Reputation: 106
• 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">
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){
}
}
</script>
</body>

```

• MSX

Reputation: 131
• 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

### #6 Tara200

Reputation: -10
• 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

### #7 Tara200

Reputation: -10
• 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:)

• MSX

Reputation: 131
• 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)];
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
//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

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

### #9 Tara200

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

## Re: Maths Game

Posted 21 March 2008 - 11:03 AM

Yes thankyou very much!!!

• MSX

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

## Re: Maths Game

Posted 22 March 2008 - 11:30 AM

You're welcome