2016-08-09 46 views
0

Hallo, hoffentlich kann mir jemand dabei helfen - wahrscheinlich etwas einfaches, das ich übersehe.Ich bekomme zufällige undefinierte Werte zurück von Json

Ich bekomme immer undefinierte Werte nach dem Zufallsprinzip - der richtige Wert erscheint manchmal und manchmal bekomme ich die "undefined".

Ich habe versucht, var q1; zu var q1 = ''; usw. zu ändern, und obwohl dies den undefinierten Wert stoppt, bekomme ich dann einen zufälligen leeren Wert.

Irgendwelche Ideen? Vielen Dank im Voraus :)

$(document).ready(function() { 

    var questionNumber = 0; 
    var questionBank = new Array(); 
    var stage = "#game1"; 
    var stage2 = new Object; 
    var questionLock = false; 
    var numberOfQuestions; 
    var score = 0; 

    $.getJSON('activity.json', function(data) { 

     for (i = 0; i < data.quizlist.length; i++) { 
      questionBank[i] = new Array; 
      questionBank[i][0] = data.quizlist[i].question; 
      questionBank[i][1] = data.quizlist[i].option1; 
      questionBank[i][2] = data.quizlist[i].option2; 
      questionBank[i][3] = data.quizlist[i].option3; 
      questionBank[i][4] = data.quizlist[i].option4; 
     } 
     numberOfQuestions = questionBank.length; 

     displayQuestion(); 
     }) //gtjson 

    function displayQuestion() { 
     var rnd = Math.random() * 4; 
     rnd = Math.ceil(rnd); 
     var q1; 
     var q2; 
     var q3; 
     var q4; 

     if (rnd == 1) { 
      q1 = questionBank[questionNumber][1]; 
      q2 = questionBank[questionNumber][2]; 
      q3 = questionBank[questionNumber][3]; 
      q4 = questionBank[questionNumber][4]; 
     } 
     if (rnd == 2) { 
      q2 = questionBank[questionNumber][1]; 
      q3 = questionBank[questionNumber][2]; 
      q4 = questionBank[questionNumber][3]; 
      q1 = questionBank[questionNumber][4]; 
     } 
     if (rnd == 3) { 
      q3 = questionBank[questionNumber][1]; 
      q4 = questionBank[questionNumber][2]; 
      q1 = questionBank[questionNumber][3]; 
      q3 = questionBank[questionNumber][4]; 
     } 
     if (rnd == 4) { 
      q4 = questionBank[questionNumber][1]; 
      q1 = questionBank[questionNumber][2]; 
      q2 = questionBank[questionNumber][3]; 
      q3 = questionBank[questionNumber][4]; 
     } 

     $(stage).append('<div class="questionText">' + questionBank[questionNumber][0] + '</div><div id="1" class="option">' + q1 + '</div><div id="2" class="option">' + q2 + '</div><div id="3" class="option">' + q3 + '</div><div id="4" class="option">' + q4 + '</div>'); 

     $('.option').click(function() { 
      if (questionLock == false) { 
      questionLock = true; 
      //correct answer 
      if (this.id == rnd) { 
       $(stage).append('<div class="feedback1">CORRECT</div>'); 
       score++; 
      } 
      //wrong answer 
      if (this.id != rnd) { 
       $(stage).append('<div class="feedback2">INCORRECT</div>'); 
      } 
      setTimeout(function() { 
       changeQuestion() 
      }, 1000); 
      } 
     }) 
     } //display question 

    function changeQuestion() { 

     questionNumber++; 

     if (stage == "#game1") { 
      stage2 = "#game1"; 
      stage = "#game2"; 
     } else { 
      stage2 = "#game2"; 
      stage = "#game1"; 
     } 

     if (questionNumber < numberOfQuestions) { 
      displayQuestion(); 
     } else { 
      displayFinalSlide(); 
     } 

     $(stage2).animate({ 
      "right": "+=800px" 
     }, "slow", function() { 
      $(stage2).css('right', '-800px'); 
      $(stage2).empty(); 
     }); 
     $(stage).animate({ 
      "right": "+=800px" 
     }, "slow", function() { 
      questionLock = false; 
     }); 
     } //change question 

    function displayFinalSlide() { 

     $(stage).append('<div class="questionText">You have finished the quiz!<br><br>Total questions: ' + numberOfQuestions + '<br>Correct answers: ' + score + '</div>'); 

     } //display final slide 


    }); //doc ready 

JSON:

{"quizlist":[ 

    { 
    "question":"1. Sam wants to be sure to listen carefully to what the influencee says during an important conversation. What are the correct steps to listen effectively?", 
     "option1":"Ask questions, listen to the answers, and acknowledge what is said.", 
     "option2":"Listen for feelings and facts, clarify, and share a personal point of view.", 
     "option3":"Share a personal point of view, clarify, and ask for feedback.", 
     "option4":"Use closed questions to get the facts, clarify, and confirm what is said." 
    }, 

    { 
    "question":"2. A team member tried to convince colleagues that the best course of action to increase team performance was to ask for volunteer coaches to train newer team members. The team member presented plans for the coaching program on three different occasions, but was not able to persuade any colleagues to support the plan. Now, the team member wants to learn more about how others on the team recommend improving performance. What approach will have the most impact to understand how others view the situation?", 

    "option1":"Meet with the colleagues as a group and ask them to discuss their point of view about the problem and the proposed solution.", 
    "option2":"Explain that he/she has given up on their ideas and will go along with anything the group is willing to do.", 
    "option3":"Meet with colleagues as a group and paint a picture of the future if the problem is not addressed immediately.", 
    "option4":"Suggest that colleagues send their comments and ideas directly to the team member. " 
    }, 

    { 
    "question":"3. A supervisor explained the reasons for a new procedure and started a discussion with the team. The supervisor said, “I expect you to adopt the new procedure immediately and don’t want us to waste time. If you do this in the next two weeks, I will be able to tell the leadership team that we have met our objective ahead of schedule. If you don’t do this, there could be problems.” Which influencing style did this supervisor use? ", 

     "option1":"Dominating. The supervisor is pushing the team by putting pressure on the team.", 
     "option2":"Asserting. The supervisor is describing what she/he wants and pushing the team by explaining his/her reasoning for the proposal.", 
     "option3":"Storytelling. The supervisor is pulling the team together by presenting a picture of the common elements.", 
     "option4":"Responding. The supervisor is pulling team members toward the idea by explaining is thinking." 
    }  
] 
} 
+0

Es ist einfacher, Fehler zu erkennen, wenn Sie Ihren Code richtig formatieren, wie bei meiner Bearbeitung (ich habe nur den Leerraum geändert). – nnnnnn

+0

Danke - Entschuldigung - erster Beitrag - ziemlich dringend - wird in Zukunft erinnern. – Adam

+0

Sorry realisierte gerade, dass du den eigentlichen Code meintest - ja, ziemlich unordentlich, sorry! - Ich habe es heute Morgen von jemand anderem passiert, viel einfacher, mit Ihrer Formatierung zu sehen - danke nochmal – Adam

Antwort

1

Der Fehler ist diese Zeile:

if(rnd==3){q3=questionBank[questionNumber][1];q4=questionBank[questionNumber][2];q1=questionBank[questionNumber][3];q3=questionBank[questionNumber][4];} 

Sie q3 zweimal definiert haben, aber nicht q2 überhaupt definiert.

Ich würde etwas mehr defensive Programmierung vorschlagen, so dass Fehler wie diese schwerer zu machen sind.

+0

Erstaunlich danke - werde nach weiteren Möglichkeiten suchen, aber das ist für einen wirklich schnellen Prototyp. Danke noch einmal :) – Adam