/    Sign up×
Community /Pin to ProfileBookmark

problem with adding user scores in javascript game

Hi

I’m having a little problem getting my scoring system to work in my javascript maths game. After the person has finished answering all questions, they should get all their answers and show whether they are correct or not. I have got this part working, but the scoring system is not showing, so the person cannot see their score at the end. A section of my code is shown below:

[CODE]
<html>
<head>
<title>Mathematics Quiz</title>
<link rel=”stylesheet” type=”text/css” href=”quiz.css”>
<script type=”text/javascript”>
quest1 = Math.floor(Math.random()*101);
quest2 = Math.floor(Math.random()*101);
quest3 = Math.floor(Math.random()*101);
quest4 = Math.floor(Math.random()*101);
var question1 = prompt(quest1+ “+” +quest2);
var question2 = prompt(quest3+ “+” +quest4);
</script>
</head>
<body>
<h1>Mathematics Quiz</h1>
<script type=”text/javascript”>
score = 0;
if (question1 == quest1+quest2)
{
document.write (‘<p class=”correct”>’+ quest1+’+’+quest2 +’= ‘+ question1 + ‘ …Well Done!</p>’);
score++;
score++;
}
else
{
document.write (‘<p class=”incorrect”>’+quest1+’+’+quest2 +’= ‘+ question1+ ‘ …Oh dear!, The correct answer is ‘+ (quest1 + quest2)+ ‘</p>’);
}

if (question2 == quest3+quest4)
{
document.write (‘<p class=”correct”>’+quest3+’+’+quest4 +’= ‘+ question2+ ‘ …Well Done!</p>’);
score++;
score++;
}
else
{
document.write (‘<p class=”wrong”>’+quest3+’+’+quest4 +’= ‘+ question2+ ‘ …Oh Dear!, The correct answer is ‘ + (quest3 + quest4)+ ‘</p>’);
}

if (score == 4)
{
document.write (‘<p class=”fourm”>you scored: ‘+ score+ ‘</p>’);
document.bgColor=”gold”;
}

if (score == 2)
{
document.write (‘<p class=”twom”>you scored: ‘+ score+ ‘</p>’);
document.bgColor=”silver”;
}

if (score == 0)
{
document.write (‘<p class=”zerom”>you scored: ‘+ score+ ‘</p>’);
document.bgColor=”darkred”;
}
</script>
</body>
</html>
[/CODE]

Can you tell me what is going wrong and how I correct this so the person can see what they have scored?

Thanks in advance

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Farel321Mar 18.2009 — You shouldn't have your question asking portion in the header of your document. That section is reserved for declarations. However that does not hinder the ability for your program to show their scores.

The reason why you are not getting your scores at the end is because you are not declaring your quest1 quest2 etc. as variables. Therefore their value is not being retained.

That being said all you need to do is put var in front of each of your quest1 quest2 etc.

so
[CODE]
<html>
<head>
<title>Mathematics Quiz</title>
<link rel="stylesheet" type="text/css" href="quiz.css">
<script type="text/javascript">
var quest1 = Math.floor(Math.random()*101);
var quest2 = Math.floor(Math.random()*101);
var quest3 = Math.floor(Math.random()*101);
var quest4 = Math.floor(Math.random()*101);
var question1 = prompt(quest1+ "+" +quest2);
var question2 = prompt(quest3+ "+" +quest4);
</script>
</head>
<body>
<h1>Mathematics Quiz</h1>
<script type="text/javascript">
score = 0;
if (question1 == quest1+quest2)
{
document.write ('<p class="correct">'+ quest1+'+'+quest2 +'= '+ question1 + ' ...Well Done!</p>');
score++;
score++;
}
else
{
document.write ('<p class="incorrect">'+quest1+'+'+quest2 +'= '+ question1+ ' ...Oh dear!, The correct answer is '+ (quest1 + quest2)+ '</p>');
}

if (question2 == quest3+quest4)
{
document.write ('<p class="correct">'+quest3+'+'+quest4 +'= '+ question2+ ' ...Well Done!</p>');
score++;
score++;
}
else
{
document.write ('<p class="wrong">'+quest3+'+'+quest4 +'= '+ question2+ ' ...Oh Dear!, The correct answer is ' + (quest3 + quest4)+ '</p>');
}

if (score == 4)
{
document.write ('<p class="fourm">you scored: '+ score+ '</p>');
document.bgColor="gold";
}

if (score == 2)
{
document.write ('<p class="twom">you scored: '+ score+ '</p>');
document.bgColor="silver";
}

if (score == 0)
{
document.write ('<p class="zerom">you scored: '+ score+ '</p>');
document.bgColor="darkred";
}
</script>
</body>
</html>
[/CODE]
×

Success!

Help @rushhour2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...