$(function() {
  $('#quizform').submit(function() {
    var questions = $('.question').length;
    
    var numRight = 0;
    $('.question input').each(function(i, answer) {
      answer = $(answer);
      if ( answer.is(':checked') ) {
        var correct = answer.val() == 'right';
        
        if (correct) {
          numRight++;
        }
        
        answer.parent().css('backgroundColor', correct ? 'green' : 'red');
      } else {
        answer.parent().css('backgroundColor', '');
      }
    });
    
    $('#results').text(numRight + " questions correct out of " + questions + ": " + Math.round(numRight * 100.0/questions) + "%")
    
    return false;
  });
})

