Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do i calculate the score in this code? I probably need to use the for loop. Thanks

// Questions
var questions = [
    'What do chickens think we taste like?',
    'Before they invented drawing boards, what did they go back to?',
    'What do sheep count when they can\'t sleep?',
    'Where in the world is Carmen Sandiego?'
];
var possibleAnswers = [
    ['Humans', 'Cats', 'Pumpkins', 'Corn'],
    ['Pen and Paper', 'Stone and Chizle', 'Cave paintings', 'Sand sculptures' ],
    ['Blades of Grass', 'Lobsters', 'Car headlights', 'Humans in underware'],
    ['Paris', 'Costa Rico', 'Cuba', 'Hong Kong']
];
var correctAnswers = [ 0, 1, 0, 3 ];
var currentQuestion = 0;

// Question Areas
var input = '<label class="col-xs-6"> <input type="radio" name="answer" value="1" /> <span>Answer</span> </label>';
var questionDisplay = $('#questionDisplay');
var possibleAnswerArea = $('#possibleAnswersArea');
var finalButton = $('#finalAnswer');

// User Information
var userName = prompt('Enter Your Name');
$('#userName').text(userName);
var currentScore = 0;
var passScore = 10;
var userScore = $('#userScore').text(currentScore);

var messageArea = $('#messageArea');

var globalIndex = -1;

nextQuestion();
nextAnswer();

function checkAnswer() {
    if (globalIndex < questions.length - 1 ) {
        nextQuestion();
        nextAnswer();
        foundMatchingBlocks();
    } else {
        gameOver();
    }
    return false;
}

function nextQuestion() {
    globalIndex++;
    questionDisplay.text(questions[globalIndex]);
    }

function nextAnswer() {
    var button = '';
    for (var i = 0; i < possibleAnswers[globalIndex].length; i++){
    button += '<label class="col-xs-6"> <input type="radio" name="answer" value="0" /> <span>' + possibleAnswers[globalIndex][i] + '</span> </label>';  
    } possibleAnswerArea.html(button);
    console.log (button);

}

function gameOver() {
    alert('All out of questions');
}

how do i calculate the correct answer. I probably have to use the for loop. Can someone help please? Thanks

1 Answer

Steven Parker
Steven Parker
229,606 Points

The score part is easy, just have a global "score" variable, and add 1 to it anytime the answer is right.

But you have some more work to do before that can help. Things like:

  • You need a loop of some kind to go through each question.
  • You reference foundMatchingBlocks but it's not defined anywhere.
  • You defined checkAnswer but you never call it.
  • The checkAnswer function doesn't seem to actually check anything (If it did, that's where you would add to your score).

Thank you!