+ `)
+ }
+}
+
+$.ajax({
+ url: options.url
+}).done((data) => {
+ const { questions , title} = data;
+
+ // Load data from past reponses
+ let quizData = loadResponses()
+ let {currentQuestion=0, responseCount=0, responses=[]} = quizData
+
+ // Append the progress bar to DOM
+ appendProgressBar()
+
+ // Append title and form to quiz
+ appendTitle(title)
+
+ // For each question of the json,
+ questions.forEach((question, i) => {
+ const {input: {type= 'input', options=[]} = {}, problem} = question
+
+ if (question.input === undefined) {
+ question.input = {
+ type: 'input'
+ }
+ }
+
+ // Construct the input depending on question type
+ let input = buildInput(i, type, options, responses)
+ appendQuestion(i, question, input)
// Show current question
- $('#quiz-form')
- .find('#question-' + currentQuestion)
- .css('display', 'block')
+ showCurrentQuestion(currentQuestion)
// Update progress bar
- $('#progress')
- .css('width', (responseCount / questions.length * 100) + '%')
- }
+ updateProgressBar(responseCount, questions)
+ })
// Add button to submit response
$('#quiz')
.append('')
// Is case all questions have been responded
- if (responseCount === questions.length) {
- $('#submit-response').css('display', 'none')
- $('#quiz').append('
Thank you for your responses.
')
- $('#quiz').append('')
- }
+ checkAllQuestionsAnswered(responseCount, questions)
// Add a reset button that will redirect to quiz start
- $resetButton = $('')
- $resetButton.on('click', function() {
- localStorage.removeItem('quiz')
- location.reload();
- })
- $('#quiz').append($resetButton)
+ addResetButton()
// Actions on every response submission
- $('#submit-response').on('click', function() {
- var $inputs = $('[name^=question_' + currentQuestion + ']')
- var question = questions[currentQuestion]
+ $('#submit-response').on('click', () => {
+
+ const $inputs = $(`[name^=question_${currentQuestion}]`)
+ let question = questions[currentQuestion]
// Behavior for each question type to add response to array of responses
- switch (question.input.type) {
- case 'checkbox':
- case 'radio':
- responses[currentQuestion] = []
- $('[name=' + $inputs.attr('name') + ']:checked').each(function(i, input) {
- responses[currentQuestion].push(input.value)
- })
- if (responses[currentQuestion].length === 0) {
- responses[currentQuestion] = null
- }
- break
- case 'inputs':
- responses[currentQuestion] = []
- $inputs.each(function(i, input) {
- responses[currentQuestion].push(input.value)
- })
- break
- default:
- responses[currentQuestion] = $inputs.val()
- }
+ responses = putResponsesIntoArray(question, $inputs, responses, currentQuestion)
// Set the current responses counter
- var responseCount = 0
- for (i = 0; i < responses.length; i++) {
- question = questions[i]
- switch (question.input.type) {
- case 'checkbox':
- case 'radio':
- case 'inputs':
- if (!!responses[i] && !!responses[i].join('')) {
- responseCount++
- }
- break
- default:
- if (!!responses[i]) {
- responseCount++
- }
- }
- }
+ let responseCount = getResponseCount(responses, questions)
// Update progress bar
- $('#progress')
- .css('width', (responseCount / questions.length * 100) + '%')
+ updateProgressBar(responseCount, questions)
// Check if question had a valid answer
- isQuestionAnswered = true
- if (!responses[currentQuestion]) {
- isQuestionAnswered = false
- }
- if (!!responses[currentQuestion] && !!responses[currentQuestion].length) {
- for (j = 0; j < responses[currentQuestion].length; j++) {
- if (!responses[currentQuestion][j]) {
- isQuestionAnswered = false
- }
- }
- }
-
- if (!isQuestionAnswered) {
+ if (!checkIfQuestionAnswered(responses, currentQuestion)) {
// Alert user of missing response
alert('You must give a response')
} else {
// Display next question
- $('#quiz-form')
- .find('#question-' + currentQuestion).css('display', 'none')
- currentQuestion = currentQuestion + 1
-
- $('#quiz-form')
- .find('#question-' + currentQuestion).css('display', 'block')
-
+ currentQuestion = displayNextQuestion(currentQuestion)
+
// If it was the las question, display final message
- if (responseCount === questions.length) {
- $('#submit-response').css('display', 'none')
- $('#quiz').append('
Thank you for your responses.
')
- $('#quiz').append('')
- }
+ checkForFinalMessage(responseCount, questions)
}
// Save current state of the quiz
- quizData.responses = responses
- quizData.responseCount = responseCount
- quizData.currentQuestion = currentQuestion
- localStorage.setItem('quiz', JSON.stringify(quizData))
+ saveResponses(responses, responseCount, currentQuestion)
})
-})
+})
\ No newline at end of file