@emsee71 wrote:
How can I show the Quiz Results in my quiz app?
Here is my code:
<div class="quiz-area" ng-show="inProgress"> <div ng-show="!quizOver"> <h2 id="question">{{question}}</h2> <ul id="options"> <li ng-repeat="option in options"> <label> <input type="radio" name="answer" value="{{option}}"> {{option}} </label> </li> </ul> <button ng-click="checkAnswer()" ng-show="answerMode">Submit</button> <div ng-show="!answerMode"> <button ng-click="nextQuestion()" class="next-question">Next</button> <span ng-show="correctAns">That is correct!</span> <span ng-show="!correctAns">Sorry, that is an incorrect answer.</span> </div> </div> <div ng-show="quizOver"> <h2>Quiz is over</h2> <button ng-click="reset()">Play again</button> </div> <div id="score"> Score: {{score}} </div> </div>
my JS Code:
app.directive('quiz', function(quizFactory) { return { restrict: 'AE', scope: {}, templateUrl: 'template.html', link: function(scope, elem, attrs) { scope.start = function() { scope.id = 0; scope.quizOver = false; scope.inProgress = true; scope.getQuestion(); }; scope.reset = function() { scope.inProgress = false; scope.score = 0; } scope.getQuestion = function() { var q = quizFactory.getQuestion(scope.id); if(q) { scope.question = q.question; scope.options = q.options; scope.answer = q.answer; scope.answerMode = true; } else { scope.quizOver = true; } }; scope.checkAnswer = function() { if(!$('input[name=answer]:checked').length) return; var ans = $('input[name=answer]:checked').val(); if(ans == scope.options[scope.answer]) { scope.score++; scope.correctAns = true; } else { scope.correctAns = false; } scope.answerMode = false; }; scope.nextQuestion = function() { scope.id++; scope.getQuestion(); } scope.reset(); } } }); app.factory('quizFactory', function() { var questions = [ { question: "Which is the largest country in the world by population?", options: ["India", "USA", "China", "Russia"], answer: 2 }, { question: "When did the second world war end?", options: ["1945", "1939", "1944", "1942"], answer: 0 }, { question: "Which was the first country to issue paper currency?", options: ["USA", "France", "Italy", "China"], answer: 3 }, { question: "Which city hosted the 1996 Summer Olympics?", options: ["Atlanta", "Sydney", "Athens", "Beijing"], answer: 0 }, { question: "Who invented telephone?", options: ["Albert Einstein", "Alexander Graham Bell", "Isaac Newton", "Marie Curie"], answer: 1 } ]; return { getQuestion: function(id) { if(id < questions.length) { return questions[id]; } else { return false; } } }; });
Posts: 1
Participants: 1