Permalink
Browse files

updated jsQuizEngine javascript to latest

  • Loading branch information...
1 parent 103f87f commit 2a6ebc68e90f2d55672ec71bede81fb6d69c647d @crpietschmann committed Mar 6, 2017
Showing with 32 additions and 13 deletions.
  1. +32 −13 jshttps://github.com/jsQuizEngine.js
@@ -1,5 +1,5 @@
// jsQuizEngine http://www.oddjack.com/?certs=crpietschmannhttps://github.com/jsQuizEngine
-// Copyright (c) 2015 Chris Pietschmann http://pietschsoft.com
+// Copyright (c) 2017 Chris Pietschmann http://pietschsoft.com
// Licensed under MIT License http://www.oddjack.com/?certs=crpietschmannhttps://github.com/jsQuizEngine/blob/master/LICENSE
(function (window, $) {
@@ -25,15 +25,26 @@
self.element.find('.question-pool').load(self.options.quizUrl, function () {
// quiz loaded into browser from HTML file
- getCurrentQuiz(self.element).find('.answer').each(function (e, i) {
- var elem = $(this),
- newAnswer = $('<label></label>').addClass('answer').append('<input type=\'checkbox\'/>').append($('<div></div>').html(elem.html()));
- if (elem.is('[data-correct]')) {
- newAnswer.attr('data-correct', '1');
- }
- elem.replaceWith(newAnswer);
+ getCurrentQuiz(self.element).find('.question').each(function (i, e) {
+ var question = $(this),
+ questionIndex = i,
+ answers = question.find('.answer'),
+ correctAnswerCount = question.find('.answer[data-correct]').length;
+
+ answers.each(function (ai, ae) {
+ var answer = $(this),
+ newAnswer = $('<label></label>').addClass('answer').append('<input type=\'checkbox\'/>').append($('<div></div>').html(answer.html()));
+ if (answer.is('[data-correct]')) {
+ newAnswer.attr('data-correct', '1');
+ }
+ if (correctAnswerCount <= 1){
+ newAnswer.find('input').attr('type','radio').attr('name','question' + questionIndex);
+ }
+ answer.replaceWith(newAnswer);
+ });
});
+
self.questionCount(getAllQuestions(self.element).length);
self.quizTitle(getCurrentQuiz(self.element).attr('data-title'));
self.quizSubTitle(getCurrentQuiz(self.element).attr('data-subtitle'));
@@ -69,6 +80,10 @@
self.currentQuestionIsLast = ko.computed(function () {
return self.currentQuestionIndex() === self.questionCount();
});
+ self.currentQuestionHasHint = ko.computed(function () {
+ var q = getQuestionByIndex(self.element, self.currentQuestionIndex());
+ return (q.find('.hint').length > 0);
+ });
self.startQuiz = function () {
// reset quiz to start state
@@ -84,6 +99,10 @@
self.movePreviousQuestion = function () {
self.currentQuestionIndex(self.currentQuestionIndex() - 1);
};
+ self.showCurrentQuestionHint = function () {
+ var q = getQuestionByIndex(self.element, self.currentQuestionIndex());
+ q.find('.hint').slideDown();
+ };
self.showCurrentQuestionAnswer = function () {
var q = getQuestionByIndex(self.element, self.currentQuestionIndex());
q.find('.answer[data-correct]').addClass('highlight');
@@ -97,15 +116,15 @@
getAllQuestions(self.element).each(function (i, e) {
var q = $(this);
if (q.find('.answer').length === (
- q.find('.answer[data-correct] > input[type=checkbox]:checked').length + q.find('.answer:not([data-correct]) > input[type=checkbox]:not(:checked)').length
+ q.find('.answer[data-correct] > input:checked').length + q.find('.answer:not([data-correct]) > input:not(:checked)').length
)) {
correctQuestions.push(q);
}
});
self.totalQuestionsCorrect(correctQuestions.length);
if (self.questionCount() !== 0) {
- self.calculatedScore(self.totalQuestionsCorrect() / self.questionCount() * 100);
+ self.calculatedScore( Math.round( (self.totalQuestionsCorrect() / self.questionCount() * 100) * 10 ) / 10 );
}
self.calculatedScoreDate(getNowDateTimeStamp());
@@ -116,7 +135,7 @@
self.calculatedScore = ko.observable(0);
self.calculatedScoreDate = ko.observable('');
self.quizPassed = ko.computed(function () {
- return self.calculatedScore() >= 70;
+ return self.calculatedScore() >= 50;
});
};
@@ -128,7 +147,7 @@
quizUrl: 'original.htm'
};
engine.fn = engine.prototype = {
- version: 0.1,
+ version: 0.2,
init: function (elem, options) {
var vm = new ViewModel(elem[0], options);
ko.applyBindings(vm, elem[0]);
@@ -137,4 +156,4 @@
engine.fn.init.prototype = engine.fn;
-})(window, jQuery);
+})(window, jQuery);

0 comments on commit 2a6ebc6

Please sign in to comment.