Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to calculate and display correct answers in ng-quiz angularjs 1

What I have tried:

$scope.isCorrect = function (question) {
$scope.correctCount = 0;
question.Options.forEach(function (option, index, array) {
if (helper.toBool(option.Selected) = option.IsAnswer) {

$scope.correctCount++;

}

});
return result;

};
Posted
Comments
OriginalGriff 17-Jan-19 8:24am    
And? Do you have a question?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 13992677 17-Jan-19 8:37am    
I'm an amateur in programming, trying to learn by modifying existing program. I am working on ng-quiz angularjs) but I discovered that everything is working fine but I expect the system to show me total scores on submit. please see the full code below for more understanding:

var quizCtrl = function ($scope, $http, helper) {
$scope.quizName = 'data/csharp.js';

//Note: Only those configs are functional which is documented at: http://www.codeproject.com/Articles/860024/Quiz-Application-in-AngularJs
// Others are work in progress.
$scope.defaultConfig = {
'allowBack': true,
'allowReview': true,
'autoMove': false, // if true, it will move to next question automatically when answered.
'duration': 0, // indicates the time in which quiz needs to be completed. post that, quiz will be automatically submitted. 0 means unlimited.
'pageSize': 1,
'requiredAll': false, // indicates if you must answer all the questions before submitting.
'richText': false,
'shuffleQuestions': false,
'shuffleOptions': false,
'showClock': false,
'showPager': true,
'theme': 'none'
}

$scope.goTo = function (index) {
if (index > 0 && index <= $scope.totalItems) {
$scope.currentPage = index;
$scope.mode = 'quiz';
}
}

$scope.onSelect = function (question, option) {
if (question.QuestionTypeId == 1) {
question.Options.forEach(function (element, index, array) {
if (element.Id != option.Id) {
element.Selected = false;
//question.Answered = element.Id;
}
});
}

if ($scope.config.autoMove == true && $scope.currentPage < $scope.totalItems)
$scope.currentPage++;
}

$scope.onSubmit = function () {
var answers = [];
$scope.questions.forEach(function (q, index) {
answers.push({ 'QuizId': $scope.quiz.Id, 'QuestionId': q.Id, 'Answered': q.Answered });
});
// Post your data to the server here. answers contains the questionId and the users' answer.
//$http.post('api/Quiz/Submit', answers).success(function (data, status) {
// alert(data);
//});
console.log($scope.questions);
$scope.mode = 'result';
}

$scope.pageCount = function () {
return Math.ceil($scope.questions.length / $scope.itemsPerPage);
};

//If you wish, you may create a separate factory or service to call loadQuiz. To keep things simple, i have kept it within controller.
$scope.loadQuiz = function (file) {
$http.get(file)
.then(function (res) {
$scope.quiz = res.data.quiz;
$scope.config = helper.extend({}, $scope.defaultConfig, res.data.config);
$scope.questions = $scope.config.shuffleQuestions ? helper.shuffle(res.data.questions) : res.data.questions;
$scope.totalItems = $scope.questions.length;
$scope.itemsPerPage = $scope.config.pageSize;
$scope.currentPage = 1;
$scope.mode = 'quiz';

$scope.$watch('currentPage + itemsPerPage', function () {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;

$scope.filteredQuestions = $scope.questions.slice(begin, end);
});
});
}
$scope.loadQuiz($scope.quizName);

$scope.isAnswered = function (index) {
var answered = 'Not Answered';
$scope.questions[index].Options.forEach(function (element, index, array) {
if (element.Selected == true) {
answered = 'Answered';
return false;
}
});
return answered;
};


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900