2017-05-21 8 views
-2

私はJava、CSS、HTMLを使ってJavaScriptでクイズアプリケーションを開発しています。私は同じ文書(Notepad ++)上のすべてのコードを必要とし、JavaScriptはHTML Idにリンクしていません。 誰かを助けることができますか?<script>をHTMLクイズにリンクするにはどうすればよいですか?

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>Quiz</title> 
 
</head> 
 
<style> 
 
    body { 
 
    background-color: #eeeeee; 
 
    } 
 
    
 
    .grid { 
 
    width: 600px; 
 
    height: 500px; 
 
    margin: 0 auto; 
 
    background-color: #fff; 
 
    padding: 10px 50px 50px 50px; 
 
    border-radius: 50px; 
 
    border: 2px solid #cbcbcb; 
 
    box-shadow: 10px 15px 5px #cbcbcb 
 
    } 
 
    
 
    .grid h1 { 
 
    font-family: sans-serif; 
 
    background-color: #57636e; 
 
    font-size: 60px; 
 
    text-align: center; 
 
    color: #fff; 
 
    padding: 2px 0px border-radius: 50px 
 
    } 
 
    
 
    .grid #question { 
 
    font-family: sans-serif; 
 
    font-size: 30px; 
 
    color: #5a6772 
 
    } 
 
    
 
    .buttons { 
 
    margin-top: 30px; 
 
    } 
 
    
 
    #btn0, 
 
    #btn1, 
 
    #btn2, 
 
    #btn3 { 
 
    background-color: #778897; 
 
    width: 250px; 
 
    font-size: 20px; 
 
    color: #fff; 
 
    border: 1px solid #1d3c6a; 
 
    border-radius: 50px; 
 
    margin: 10px 40px 10px 0px; 
 
    padding: 10px 10px; 
 
    } 
 
    
 
    #progress { 
 
    color: #2b2b2b; 
 
    font-size: 18px; 
 
    } 
 
    
 
    #btn0:hover, 
 
    #btn1:hover, 
 
    #btn2:hover, 
 
    #btn3:hover { 
 
    cursor: pointer; 
 
    background-color: #57636e; 
 
    } 
 
    
 
    #btn0:focus, 
 
    #btn1:focus, 
 
    #btn2:focus, 
 
    #btn3:focus { 
 
    outline: 0; 
 
    } 
 
</style> 
 

 
<body> 
 
    <div class="grid"> 
 
    <div id="quiz"> 
 
     <h1>Quiz</h1> 
 
     <hr style="margin-bottom: 20px"> 
 

 
     <p id="question"></p> 
 

 
     <div class="buttons"> 
 
     <button id="btn0"><span id="choice0"></span></button> 
 
     <button id="btn1"><span id="choice1"></span></button> 
 
     <button id="btn2"><span id="choice2"></span></button> 
 
     <button id="btn3"><span id="choice3"></span></button> 
 
     </div> 
 

 
     <hr style="margin-top: 50px"> 
 
     <footer> 
 
     <p id="progress">Question x of y.</p> 
 
     </footer> 
 
    </div> 
 
    </div> 
 
    <script language="javascript"> 
 
    function Quiz(questions) { 
 
     this.score = 0; 
 
     this.questions = questions; 
 
     this.questionIndex = 0; 
 
    } 
 
    Quiz.prototype.getQuestionIndex = function() { 
 
     return this.questions(this.questionIndex); 
 
    } 
 
    Quiz.prototype.isIndex = function() { 
 
     return this.questions.length === this.questionIndex; 
 
    } 
 
    Quiz.prototype.guess = function(answer) { 
 
     this.questionIndex++; 
 
     if (this.getQuestionIndex().correctAnswer(answer)) { 
 
     this.score++; 
 
     } 
 
    } 
 
    //--------------------------------------------------------------- 
 
    //Questions.js 
 
    function Question(text, choices, answer) { 
 
     this.text = text; 
 
     this.choices = choices; 
 
     this.answer = answer; 
 
    } 
 
    Question.prototype.correctAnswer = function(choice) { 
 
     return choice === this.answer; 
 
    } 
 
    //--------------------------------------------------------------- 
 
    //Apps 
 
    function populate() { 
 
     if (quiz.isEnded()) { 
 
     showScores(); 
 
     } else { 
 
     var element = document.getElementById("question"); 
 
     element.innerHTML = quiz.getQuestionIndex().text; 
 

 
     var choices = quiz.getQuestionIndex().choices; 
 
     for (var i = 0; i < choices.length; i++) { 
 
      var element = document.getElementById("choice" + i); 
 
      element.innerHTML = choices[i]; 
 
      guess("btn" + i, choices[i]); 
 
     } 
 

 
     } 
 
    }; 
 

 
    function guess() { 
 
     var button = document.getElementById(id); 
 
     button.onclick = function() { 
 
     quiz.guess(guess); 
 
     populate(); 
 
     } 
 
    } 
 

 
    function showScores() { 
 
     var gameOverHtml = "<h1>Result</h1>"; 
 
     gameOverHtml += "<h2 id='score'> Your scores: " + quiz.score + "</h2>"; 
 
     var element = document.getElementById("quiz"); 
 
     element.innerHTML = gameOverHtml; 
 
    } 
 
    var questions = [ 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
    ]; 
 
    var quiz = new Quiz(questions); 
 
    populate(); 
 
    </script> 
 
</body> 
 

 
</html>

多くのおかげ

+0

*スクリプト要素の言語*属性はHTML 4で廃止されましたし、削除されました:だから最後に、あなたのquestionIndexは、アレイは、このように、未定義の質問があり、その後1大きいですずっと前に。 – RobG

答えて

3

このアプリケーションでの問題の多くが欠落しているパラメーターから、不足している機能に至るまで、あります:

ここ

コードです

クイズにisEndedという機能がないようです。

私はそれがすべての質問が求められているときであることを意味したと仮定:

Quiz.prototype.isEnded = function() { 
    return this.questionIndex === this.questions.length; 
} 

this.questions(this.questionIndex)。あなたは一連の質問を渡しています。ですから、this.questionsは実際は配列です。

Quiz.prototype.getQuestionIndex = function() { 
    return this.questions[this.questionIndex]; 
} 

は、あなたが本当に内側のスパンを必要としない、そして実際に、それは少し簡単に答えを出すためになります:私はあなたが望んでいた疑いがある

<button id="btn0"></button> 
    <button id="btn1"></button> 
    <button id="btn2"></button> 
    <button id="btn3"></button> 

あなたの移入のレンダリングの選択のセクションを変更しますTo:

for (var i = 0; i < choices.length; i++) { 
     var element = document.getElementById("btn" + i); 
     element.innerHTML = choices[i]; 
     guess("btn" + i, choices[i]); 
    } 

推定関数は、パラメータ

から欠落している id変数を定義していません
function guess(id) { 
    var button = document.getElementById(id); 
    button.onclick = function(event) { 
     quiz.guess(this.innerHTML); 
     populate(); 
    } 
} 

推測を確認するときは、回答を確認する前に増分しています。

Quiz.prototype.guess = function(answer) {  
    if (this.getQuestionIndex().correctAnswer(answer)) { 
    this.score++; 
    } 

    this.questionIndex++; 
} 

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>Quiz</title> 
 
</head> 
 
<style> 
 
    body { 
 
    background-color: #eeeeee; 
 
    } 
 
    
 
    .grid { 
 
    width: 600px; 
 
    height: 500px; 
 
    margin: 0 auto; 
 
    background-color: #fff; 
 
    padding: 10px 50px 50px 50px; 
 
    border-radius: 50px; 
 
    border: 2px solid #cbcbcb; 
 
    box-shadow: 10px 15px 5px #cbcbcb 
 
    } 
 
    
 
    .grid h1 { 
 
    font-family: sans-serif; 
 
    background-color: #57636e; 
 
    font-size: 60px; 
 
    text-align: center; 
 
    color: #fff; 
 
    padding: 2px 0px border-radius: 50px 
 
    } 
 
    
 
    .grid #question { 
 
    font-family: sans-serif; 
 
    font-size: 30px; 
 
    color: #5a6772 
 
    } 
 
    
 
    .buttons { 
 
    margin-top: 30px; 
 
    } 
 
    
 
    #btn0, 
 
    #btn1, 
 
    #btn2, 
 
    #btn3 { 
 
    background-color: #778897; 
 
    width: 250px; 
 
    font-size: 20px; 
 
    color: #fff; 
 
    border: 1px solid #1d3c6a; 
 
    border-radius: 50px; 
 
    margin: 10px 40px 10px 0px; 
 
    padding: 10px 10px; 
 
    } 
 
    
 
    #progress { 
 
    color: #2b2b2b; 
 
    font-size: 18px; 
 
    } 
 
    
 
    #btn0:hover, 
 
    #btn1:hover, 
 
    #btn2:hover, 
 
    #btn3:hover { 
 
    cursor: pointer; 
 
    background-color: #57636e; 
 
    } 
 
    
 
    #btn0:focus, 
 
    #btn1:focus, 
 
    #btn2:focus, 
 
    #btn3:focus { 
 
    outline: 0; 
 
    } 
 
</style> 
 

 
<body> 
 
    <div class="grid"> 
 
    <div id="quiz"> 
 
     <h1>Quiz</h1> 
 
     <hr style="margin-bottom: 20px"> 
 

 
     <p id="question"></p> 
 

 
     <div class="buttons"> 
 
     <button id="btn0"><span id="choice0"></span></button> 
 
     <button id="btn1"><span id="choice1"></span></button> 
 
     <button id="btn2"><span id="choice2"></span></button> 
 
     <button id="btn3"><span id="choice3"></span></button> 
 
     </div> 
 

 
     <hr style="margin-top: 50px"> 
 
     <footer> 
 
     <p id="progress">Question x of y.</p> 
 
     </footer> 
 
    </div> 
 
    </div> 
 
    <script language="text/javascript"> 
 
    function Quiz(questions) { 
 
     this.score = 0; 
 
     this.questions = questions; 
 
     this.questionIndex = 0; 
 
    } 
 
    Quiz.prototype.getQuestionIndex = function() { 
 
     return this.questions[this.questionIndex]; 
 
    } 
 
    Quiz.prototype.isIndex = function() { 
 
     return this.questions.length === this.questionIndex; 
 
    } 
 
    
 
    Quiz.prototype.guess = function(answer) { 
 
     if (this.getQuestionIndex().correctAnswer(answer)) { 
 
     this.score++; 
 
     } 
 
     
 
     this.questionIndex++; 
 
    } 
 
    
 
    Quiz.prototype.isEnded = function() { 
 
     return this.questionIndex === this.questions.length; 
 
    } 
 
    
 
    //--------------------------------------------------------------- 
 
    //Questions.js 
 
    function Question(text, choices, answer) { 
 
     this.text = text; 
 
     this.choices = choices; 
 
     this.answer = answer; 
 
    } 
 
    
 
    Question.prototype.correctAnswer = function(choice) { 
 
     return choice === this.answer; 
 
    } 
 
    
 
    //--------------------------------------------------------------- 
 
    //Apps 
 
    function populate() { 
 
     if (quiz.isEnded()) { 
 
     showScores(); 
 
     } else { 
 
     var element = document.getElementById("question"); 
 
     element.innerHTML = quiz.getQuestionIndex().text; 
 

 
     var choices = quiz.getQuestionIndex().choices; 
 
     for (var i = 0; i < choices.length; i++) { 
 
      var element = document.getElementById("btn" + i); 
 
      element.innerHTML = choices[i]; 
 
      guess("btn" + i, choices[i]); 
 
     } 
 

 
     } 
 
    }; 
 

 
    function guess(id) { 
 
     var button = document.getElementById(id); 
 
     button.onclick = function(event) { 
 
     quiz.guess(this.innerHTML); 
 
     populate(); 
 
     } 
 
    } 
 

 
    function showScores() { 
 
     var gameOverHtml = "<h1>Result</h1>"; 
 
     gameOverHtml += "<h2 id='score'> Your scores: " + quiz.score + "</h2>"; 
 
     var element = document.getElementById("quiz"); 
 
     element.innerHTML = gameOverHtml; 
 
    } 
 
    var questions = [ 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
     new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"), 
 
    ]; 
 
    var quiz = new Quiz(questions); 
 
    populate(); 
 
    </script> 
 
</body> 
 

 
</html>

+0

ありがとうございました!それは問題をソートしました! – championj

+1

また、言語を「type = "text/javascript"」に変更することもできます – TCharb

関連する問題