2014-01-29 3 views
1

간단한 수학 게임을 위해 아래에 수정/편집 된 코드가 있습니다. 해결하고자하는 금액을 입력 한 다음 해결하십시오!수학 게임을 만들려고 시도합니다.

내가 지금 겪고있는 문제는 맨 끝에는 점수 만 표시된다는 것입니다.

점수를 표시하는 것뿐 아니라 해결하기 위해 입력 한 질문의 양을 표시하려고합니다. 따라서 올바른 "입력 된"점수의 "out of more"점수가 더 많습니다. 아래는 코드와 피들 링크입니다.

http://jsfiddle.net/justinw001/Mttw6/12/

$(function() { 
    var score = 0; 
    var questions = []; 
    $('#gen').click(function() { 
     score = 0; 
     questions = []; 
     var questionAmount = parseInt($('#inputElement').val(), 10); 
     for (var i = 0; i < questionAmount; i++) { 
      var q = { 
       x: Math.floor(Math.random() * 13), 
       y: Math.floor(Math.random() * 13) 
      }; 
      questions.push(q); 
     } 
     nextQuest(questions.pop()); 
    }); 
    $('#sub').click(function() { 
     var ans, x, y; 
     if (questions.length >= 0) { 
      ans = parseInt($('#answer').val(), 10); 
      x = parseInt($('#input1').text(), 10); 
      y = parseInt($('#input2').text(), 10); 
      if (ans === x * y) { 
       score++; 
      } 
      nextQuest(questions.pop()); 
     } 
    }); 
    var nextQuest = function (q) { 
     if (q) { 
      $('#input1').text(q.x); 
      $('#input2').text(q.y); 
      $('#answer').val(''); 
      $('#inputElement').val(questions.length); 
     } else { 
      $('#input1, #input2').text(''); 
      $('#answer, #inputElement').val(''); 
      alert(score); 
     } 
    }; 
}); 
+2

어디서 붙어 있습니까? –

+0

jsFiddle이 도움이 될 것입니다. – j08691

+0

내가 묻고있는 질문 아래에는 상단에 피들 (Fiddle)이 있습니다. – user2918151

답변

2

$(function() { 
    var score = 0; 
    var questions = []; 
    var questionAmount=0; //here 
    $('#gen').click(function() { 
     score = 0; 
     questions = []; 
     questionAmount = parseInt($('#inputElement').val(), 10); 

다음 함수 외부 questionAmount를 선언 할 때 경고 할이

alert(score+" out of "+questionAmount + " correct"); 

DEMO

+1

정말 고마워요! 이것은 많은 도움이되었습니다. – user2918151

0

Here은 당신이 원하는 바를 가지고 있습니다. var numberOfQs;을 추가 한 다음 questionAmount으로 설정하고 점수를 알려주세요.

관련 문제