2017-01-23 1 views
0

"옳은"또는 "잘못"이 아닌 크기의 출력을 제공하는 자바 스크립트 퀴즈를 작성하려고합니다. 이 퀴즈는 개인 취향, 성격 등을위한 것입니다. 그래서 "당신은 어떤 슈퍼 히어로입니까?" 이 Javascript 계산 루프는 어떻게 작동합니까? - 퀴즈의 출력 만들기

내가이 JSON을 사용하려고했습니다/여기 Javascrpt : https://github.com/jbierfeldt/bf-quiz/blob/master/bf-quiz.js

은 정말 calcResult에 끊었하고있다. 나는 그것을 디버깅하려고 노력하고 있었고, 그것이 무엇을하는지 알 수 없다. 개별 그룹의 가장 높은 숫자를 추적하고 highest을 입력하고 더 큰 것이 나오면 highest_score 만 수정합니까? 이 출력이 정의 된 숫자 결과 응답과 어떻게 정렬되는지 알 수 없습니다.

calcResult = function calcResult() { 
    var highest = 0; 
    for (var i = 0; i < results.length; i++) { 
     results[i].countof = 0; 
     for (var j = 0; j < userAnswers.length; j++) { 
      if (userAnswers[j] == results[i].result.id) { 
       results[i].countof++; 
      } 
     } 
     if (results[i].countof > highest) { 
      highest = results[i].countof; 
      highest_score = results[i]; 
     } 
    } 
    //disable the inputs after the quiz is finished 
    writeResult(); 
    disableAnswers(); 
}, 

답변

1

매우 간단합니다. 나는 (약간 일반적인) 주석을 코드에 추가했다.

대부분의 사용자가 답변으로 선택한 결과를 찾습니다 (어떤 결과가 사용 사례에 해당하는지).

calcResult = function calcResult() { 
    // used to keep track of the maximum userAnswers for one result 
    var highest = 0; 

    // iterate through all results... 
    for (var i = 0; i < results.length; i++) { 

     // used to count how many userAnswers exist for this result 
     results[i].countof = 0; 
     // iterate through userAnswers and... 
     for (var j = 0; j < userAnswers.length; j++) { 
      // find userAnswers that are for current result 
      if (userAnswers[j] == results[i].result.id) { 
       // if we are here, we found a userAnswer for the 
       // current result. Therefore, we increment the 
       // number of userAnswers for the result. 
       results[i].countof++; 
      } 
     } 

     // check if the current result has the highest number of  
     // userAnswers so far 
     if (results[i].countof > highest) { 
      // if so, set the new maximum number of userAnswers per 
      // result to the number of userAnswers of the current 
      // result 
      highest = results[i].countof; 

      // store this result as the one with the most userAnswers 
      highest_score = results[i]; 
     } 
    } 
    //disable the inputs after the quiz is finished 
    writeResult(); 
    disableAnswers(); 
}, 
+0

그래, 정말 혼란 스러웠던 점 중 하나는 기본 JSON의 마지막 질문에 -1과 5의 두 가지 옵션 만 있다는 사실이었습니다. 즉 -1 응답은 문자 그대로 아무것도하지 않습니다. 나는 또한 "최고"(또는 그와 비슷한 것, 나는 정말로 j를 모르겠다.) 그리고 많은 수의 시간에 나는 25와 같은 숫자를 얻었으며, 10 개의 모든 대답에 0을 가졌을 때의 값을 가지고 있었다. – jonbon

+0

만약 내가 퀴즈 사용 사례를 올바르게 이해하고, 최고는 단지 대부분의 사용자가 얻은 결과 유형을 참조합니다. 예를 들어 https://github.com/jbierfeldt/bf-quiz/blob/master/bf-json/quiz_image.json의 경우 'highest_score = 1'은 대부분의 사용자가 'Max Palevsky'및 'highest = 25'임을 의미합니다. 25 명의 사용자가 'Max Palevsky'라는 것을 의미합니다. – fjc

+0

그래서 명확히 말하면, 답변의 계산은 "id"가 가장 많이 응답 한 것을 세고 가장 많이 사용하는 ID의 Quiz 결과 아래에 답변을 표시합니까? 그것이 내가 할 것이라고 생각한 것이지만, 그것을 테스트 할 때, 그것을 성취하는 것 같지 않습니다. – jonbon

0

highest은 점수의 수치입니다. highest_score은 가장 높은 점수를 가진 참조 된 개체입니다. 그것은 몇몇면에서 정말로 중복 된 코드입니다.

if (!highest_score || results[i].countof > highest_score.countof) { 
     highest_score = results[i]; 
    } 

을 그리고 당신은 전부 highest 변수를 제거 할 수 있습니다, 대신 highest_scorehighest_score.countof 참조 : 마지막 if 문은 다음과 같이 정제 할 수있다. 예 :

calcResult = function calcResult() { 
    for (var i = 0; i < results.length; i++) { 
     results[i].countof = 0; 
     for (var j = 0; j < userAnswers.length; j++) { 
      if (userAnswers[j] == results[i].result.id) { 
       results[i].countof++; 
      } 
     } 
     if (!highest_score || results[i].countof > highest_score.countof) { 
      highest_score = results[i]; 
     } 
    } 
    //disable the inputs after the quiz is finished 
    writeResult(); 
    disableAnswers(); 
}, 

그러나 나는 그렇게하지 않을 것입니다. 라이브러리는 아마도 코드의 다른 곳에서 highest을 참조합니다.

+0

정말 혼란 스러웠던 점 중 하나는 기본 JSON (여기서는 https://github.com/jbierfeldt/bf-quiz/blob/master/bf-json/BuildingsQuiz2.json)의 마지막 질문에 두 가지 옵션 만 있다는 사실이었습니다 , -1과 5. 그것은 -1 답이 말 그대로 아무것도하지 않는다는 것을 의미합니까 (일회용 답)? 나는 또한 "최고"(또는 그와 비슷한 것, 나는 j를 알지 못함)의 문서를 인쇄하고 있었고 25 번, 10 번과 같은 숫자를 얻었을 때 나는 모든 대답이 0 – jonbon

관련 문제