-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
22 lines (22 loc) · 7.16 KB
/
quiz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(function() { var questions = [{
question: "Consider the following variable declaration:<br><br>double a = 3.64;<br><br>Which of the following statements will round the double to the nearest integer? Meaning, round up if the decimal value is .5 or greater, otherwise round down.",
choices: ["int nearestInt = (int) a;", "int nearestInt = (int) a + 0.5;", "int nearestInt = Math.abs(a);", "int nearestInt = (int) a – 0.5;", "int nearestInt = Math.random() + a;"],
correctAnswer: 1
}, {
question: "A box company sells boxes according to the following chart:<br><br>up to 10 boxes = $1.00 ea<br>10 to 25 boxes = $0.75 ea<br>over 25 boxes = $0.50 ea<br><br>An order of 14 boxes will cost (10 * $1.00) + (4 * $0.75) = $13.00, and an order of 28 boxes will cost (10 * $1.00) + (15 * $0.75) + (3 * $0.50) = $22.75<br><br>A method is written that takes in a parameter for the total number of boxes desired and returns the cost of the order. Which data set has the minimum set if inputs that would be required to adequately test this method?",
choices: ["10, 25", "5, 15, 30", "5, 10, 15, 25, 30", "5, 10, 15, 25, 30, 35", "5, 10, 15, 20, 25, 30, 40"],
correctAnswer: 2
}, {
question: "Assuming that array is filled with integers, what would be the result of the following code?<br><br>int total = array[0], i = 0;<br>while(i < array.length)<br>{<br> i++;<br> total += array[i];<br>}<br>",
choices: ["total will contain the sum of array[0] to array[array.length – 1]", "total will contain the sum of array[0] to array[array.length]", "total will contain the sum of array[1] to array[array.length – 1]", "The loop will not execute", "A runtime error will occur"],
correctAnswer: 4
}, {
question: "The following code is intended to randomize and array of integers called nums. However, it does not work correctly.<br><br>Line 1: public void shuffle()<br>Line 2: {<br>Line 3: for(int i = TOTAL_NUMS; i > 0; i--)<br>Line 4: {<br>Line 5: int randomPosition = (int) (Math.random() * (k + 1));<br>Line 6: int temp = nums[i];<br>Line 7: nums[k] = nums[randomPosition];<br>Line 8: nums[randomPosition] = temp;<br>Line 9: }<br>Line 10: }<br><br>Which of the following changes will correct the method?",
choices: ["Replace line 3 with <br>for(int i = TOTAL_NUMS; i >= 0; i—)", "Replace line 3 with <br>for(int i = TOTAL_NUMS - 1; i > 0; i—)", "Replace line 3 with <br>for(int i = 1; i <= TOTAL_NUMS; i++)", "Replace line 5 with <br>int randomPosition = (int) (Math.random() * k);", "Replace lines 6 – 8 with<br>int temp = nums[randomPosition];<br>nums[randomPosition] = nums[i];<br>nums[i] = temp;"],
correctAnswer: 1
}, {
question: "What best describes the precondition of a method?",
choices: ["It describes the conditions that must be true at the time the method is called.", "It initializes the parameters of a method.", "It describes the effect of a method on its postcondition.", "It explains what the method does.", "It states what the initial values of the local variables in the method must be."],
correctAnswer: 0
}];
var questionCounter = 0; var selections = []; var quiz = $('#quiz'); displayNext(); $('#next').on('click', function (e) { e.preventDefault(); if(quiz.is(':animated')) { return false; } choose(); if (isNaN(selections[questionCounter])) { alert('Please make a selection!'); } else { questionCounter++; displayNext(); } }); $('#prev').on('click', function (e) { e.preventDefault(); if(quiz.is(':animated')) { return false; } choose(); questionCounter--; displayNext(); }); $('#start').on('click', function (e) { e.preventDefault(); if(quiz.is(':animated')) { return false; } questionCounter = 0; selections = []; displayNext(); $('#start').hide(); }); $('.button').on('mouseenter', function () { $(this).addClass('active'); }); $('.button').on('mouseleave', function () { $(this).removeClass('active'); }); function createQuestionElement(index) { var qElement = $('<div>', { id: 'question' }); var header = $('<h2>Question ' + (index + 1) + ':</h2>'); qElement.append(header); var question = $('<p>').append(questions[index].question); qElement.append(question); var radioButtons = createRadios(index); qElement.append(radioButtons); return qElement; } function createRadios(index) { var radioList = $('<ul>'); var item; var input = ''; for (var i = 0; i < questions[index].choices.length; i++) { item = $('<li>'); input = '<input type="radio" name="answer" value=' + i + ' />'; input += convertToLetter(i) + ':<br>'; input += questions[index].choices[i]; input += '<hr size=2>'; item.append(input); radioList.append(item); } return radioList; } function choose() { selections[questionCounter] = +$('input[name="answer"]:checked').val(); } function displayNext() { quiz.fadeOut(function() { $('#question').remove(); if(questionCounter < questions.length){ var nextQuestion = createQuestionElement(questionCounter); quiz.append(nextQuestion).fadeIn(); if (!(isNaN(selections[questionCounter]))) { $('input[value='+selections[questionCounter]+']').prop('checked', true); } if(questionCounter === 1){ $('#prev').show(); } else if(questionCounter === 0){ $('#prev').hide(); $('#next').show(); } }else { var scoreElem = displayScore(); quiz.append(scoreElem).fadeIn(); $('#next').hide(); $('#prev').hide(); $('#start').show(); } }); } function displayScore() { var score = $('<p>',{id: 'question'}); var numCorrect = 0; for (var i = 0; i < selections.length; i++) { if (selections[i] === questions[i].correctAnswer) { numCorrect++; } } score.append('<br>You got ' + numCorrect + ' questions out of ' + questions.length + ' correct.<hr size=2>'); for(var i = 0; i < questions.length; i++){ score.append('<br> Question: ' + (i + 1) + '<br>'); score.append('<br>' + questions[i].question + '<br>'); for(var j = 0; j < questions[i].choices.length; j++){ score.append(convertToLetter(j) + ":<br>" + questions[i].choices[j] + '<br>'); } score.append('<br> Your Answer: ' + convertToLetter(selections[i]) + '<br>'); score.append('<br> Correct Answer: ' + convertToLetter(questions[i].correctAnswer) + "<hr size=2>"); } return score; }})();function convertToLetter(i){ var letter; switch(i){ case 0: letter = 'A'; break; case 1: letter = 'B'; break; case 2: letter = 'C'; break; case 3: letter = 'D'; break; case 4: letter = 'E'; break; } return letter; }