We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Maths game help

having abit of trouble with this code here is wot i want to do but dont seem to be working like that can someone help please..

thanks

Promt the user to answer the 10 questions, record the answers and compare them to the correct answers. Once the user has answered all 10 questions, display on the page each question, the users's answer and a message saying whether the answer was correct or not. In case of incorrect answer, also display the correct answer.

<html>
<head>

<link rel="stylesheet" type="text/css" href="style.css">

<style>

</style>
</head>
<body>
<h1><font color="blue">Maths Game</h1></font>

<script type="text/javascript">

var ua = new Array();
var ca = new Array();
var xa = new Array();
var ya = new Array();
var opa = new Array();
var dispa = new Array();

var ops = ["+","-","*"];


function showResult()
{
var cnt = dispa.length;
for(var i=0;i<cnt;i++)
{
 document.write("<br />"+dispa[i]+ " = " + ua[i]);
 if(ua[i]!=ca[i])
 {
  document.write(" is incorrect - <span style='color:red'>The correct answer is " + ca[i] + "</span>");
 }
 else
 {
  document.write(" <span style='color:green'>The answer is correct</span>");
  score += 2;
 }
}
ua = new Array();
ca = new Array();
xa = new Array();
ya = new Array();
opa = new Array();
dispa = new Array();

}


for(var i=0;i<10;i++)
{
 var x = Math.floor(Math.random()*101);
 var y = Math.floor(Math.random()*101);
 if(x<y)
 {
  var temp = x;
  x = y;
  y = temp;
 }
 xa.push(x);
 ya.push(y);
 op = ops[Math.floor(Math.random()*ops.length)];
 var disp = x +" "+op+" "+y;
 dispa.push(disp);
 var v = prompt("How much is " + disp + " ?");
 ua.push(v);
 ca.push(Math.round(eval(disp)));
 showResult();
}
var score = 0;
document.write("<br />Your score: " + score);
if(score<10) document.body.style.backgroundColor='red';

</script>
</body>
</html>
3
Contributors
11
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
12
Views
Question
Answered
thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What isn't working?

stbuchok
Practically a Posting Shark
875 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

i want to display the questions and answers after asking them not as i answer them one by one if u see wot i mean...

thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Dear thepanther, I have seen your codes, there is nothing wrong. Just place your score variable initialization before showresult function. And change the background colour to yellow.

P.manidas
Posting Whiz
304 posts since Oct 2008
Reputation Points: 53
Solved Threads: 14
Skill Endorsements: 0

like this

ca.push(Math.round(eval(disp)));
 var score = 0;
 document.write("<br />Your score: " + score);
if(score<10) document.body.style.backgroundColor='red';
 showResult();
}
thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

and also its not showing the score at the end

thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Dear Thepanther,

I think you have not understood what i have said. Anyway, i am making for you little bit easier..

1. Replace this line "var score = 0;" from line no. 71 to 14 or 24 as per your first post
2. In the line no. 73 there is background colour as Red replace it with Yellow.

P.manidas
Posting Whiz
304 posts since Oct 2008
Reputation Points: 53
Solved Threads: 14
Skill Endorsements: 0

hi p.manidas

that worked for the score bit, but how can i change code to answer all questions first then display the results???

thanks again much appriated...

thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Dear Thepanther,

Ok, its not complicated. But, I have a question for you, have you written those codes or copied from somewhere? If you copied from somewhere and asking all help from other then you can't increase your knowledge. The better way is doing some parts yourself and ask help for some parts.

Anyway, I am telling you how to do that...


1. Declare 2 nos. of variables, one is String and other is Array.
2. Concatenate the display line into String variable like ("50*2=150" + "is incorrect - The Correct answer is 100") or ("63+11=74" + "The answer is correct").
3. After that you push the string variable's value into the Array variable.
4. In last display the Array variable's value using for loop.

Give a try to solve.....

P.manidas
Posting Whiz
304 posts since Oct 2008
Reputation Points: 53
Solved Threads: 14
Skill Endorsements: 0

thanks for saying that much appreciated, its my college work so I'm trying to get some help on it so yeh its my code but thanks as u don't learn nothing by copying someone else is code which I wouldn't do

thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Here is your needed codes. Enjoy coding and mark the thread close.

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
</style>
</head>
<body>
<h1><font color="blue">Maths Game</h1></font>

<script type="text/javascript">

var score = 0;
var ua = new Array();
var ca = new Array();
var xa = new Array();
var ya = new Array();
var opa = new Array();
var dispa = new Array();

//================
var AllCombine = new Array();
var MyString;
//================

var ops = ["+","-","*"];


function showResult()
{
var cnt = dispa.length;
for(var i=0;i<cnt;i++)
{
 //document.write("<br />"+dispa[i]+ " = " + ua[i]);
 MyString = dispa[i]+ " = " + ua[i];  //============================
 if(ua[i]!=ca[i])
 {
  //document.write(" is incorrect - <span style='color:red'>The correct answer is " + ca[i] + "</span>");
  MyString += " is incorrect - <span style='color:red'>The correct answer is " + ca[i] + "</span>";//======
 }
 else
 {
  //document.write(" <span style='color:green'>The answer is correct</span>");
  MyString += " <span style='color:green'>The answer is correct</span>"; //=================
  score += 2;
 }
AllCombine.push(MyString);//=================================
}
ua = new Array();
ca = new Array();
xa = new Array();
ya = new Array();
opa = new Array();
dispa = new Array();

}


for(var i=0;i<10;i++)
{
 var x = Math.floor(Math.random()*101);
 var y = Math.floor(Math.random()*101);
 if(x<y)
 {
  var temp = x;
  x = y;
  y = temp;
 }
 xa.push(x);
 ya.push(y);
 op = ops[Math.floor(Math.random()*ops.length)];
 var disp = x +" "+op+" "+y;
 dispa.push(disp);
 var v = prompt("How much is " + disp + " ?");
 ua.push(v);
 ca.push(Math.round(eval(disp)));
 showResult();
}
//=============================================
for(var i=0;i<10;i++)
{
document.write("<br />" + AllCombine[i]);
}
//==============================================
document.write("<br />Your score: " + score);
if(score<10) document.body.style.backgroundColor='yellow';

</script>
</body>
</html>
P.manidas
Posting Whiz
304 posts since Oct 2008
Reputation Points: 53
Solved Threads: 14
Skill Endorsements: 0

thanks very much :)

thepanther
Light Poster
30 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by P.manidas and stbuchok

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1140 seconds using 2.72MB