Javascript problem with a stored variable.

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

02-Feb-12 00:02
Hey guys. Sorry if this is too newbie of a question for this forum. I searched for Javascript help forums, and there weren't many that showed up as active. I am creating a small quiz app for a college class and have ran into a problem with a variable. This is really basic and I have just started this class so the fix is probably very simple.

What's happening is even if someone chooses one or more of the correct answers the alert box always says you got 0 right and earned a score of 0. My problem looks to be in the checkAnswers function because the score variable doesn't seem to be adding 1 even if an answer is correct. Any help would be much appreciated, thank you. Here is the code:



var score = 0;
var finalScore = 0;
var correctOne = 'Maxim';
var correctTwo = 3;
var correctThree = 'Sephiroth';
var correctFour = 8;
var correctFive = 'Up, Up, Down, Down, Left, Right, Left, Right, B, A, Start';
var answerOne;
var answerTwo;
var answerThree;
var answerFour;
var answerFive;
var a;
var b;
var c;
var d;
var e;
var givenAnswerOne;
var givenAnswerTwo;
var givenAnswerThree;
var givenAnswerFour;
var givenAnswerFive;


function getUserAnswers() {
var answerOne = document.getElementById('lufia').selectedIndex;
var a = document.getElementById('lufia').options;
var givenAnswerOne = a[answerOne].text;
//alert("You chose " + givenAnswer);

var answerTwo = document.getElementById('mario').selectedIndex;
var b = document.getElementById('mario').options;
var givenAnswerTwo = b[answerTwo].text;

var answerThree = document.getElementById('ff7').selectedIndex;
var c = document.getElementById('ff7').options;
var givenAnswerThree = c[answerThree].text;

var answerFour = document.getElementById('diablo').selectedIndex;
var d = document.getElementById('diablo').options;
var givenAnswerFour = d[answerFour].text;

var answerFive = document.getElementById('superc').selectedIndex;
var e = document.getElementById('superc').options;
var givenAnswerFive = e[answerFive].text;
}

function checkAnswers() {
if (givenAnswerOne == correctOne) {
var score = score + 1;
//alert('I got answer one.');
}
if (givenAnswerTwo == correctTwo) {
var score = score + 1;
//alert('I got answer two.');
}
if (givenAnswerThree == correctThree) {
var score = score + 1;
//alert('I got answer three.');
}
if (givenAnswerFour == correctFour) {
var score = score + 1;
//alert('I got answer four.');
}
if (givenAnswerFive == correctFive) {
var score = score + 1;
//alert('I got answer five.');
}
}

function displayScore() {
var finalScore = score / 1;
alert('You got ' + score + ' right, and earned a score of ' + finalScore);
}

function submitQuiz() {
getUserAnswers();
checkAnswers();
displayScore();
}


[Edited by janix2008 on 02-Feb-12 00:06]
02-Feb-12 23:08
@janix2008: For one thing, you have used the 'var' keyword to create new local variables inside your getUserAnswers() function. These variables will be separate from the global variables at the top of your code. The same goes for "score" inside checkAnswers():


function checkAnswers() {
if (givenAnswerOne == correctOne) {
var score = score + 1;


Your line "var score = score + 1" creates a brand new local variable called "score" that is separate from the "score" global variable at the top of your code.

So remove those 'var' keywords, unless you really want to create variables that are local to each function.

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated