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>
Member Avatar for stbuchok

What isn't working?

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

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.

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();
}

and also its not showing the score at the end

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.

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

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

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

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>

thanks very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.