Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (154 loc) · 4.42 KB
/
Copy pathindex.js
File metadata and controls
169 lines (154 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//Current line
var CurrentId = undefined;
var inputValues = [];
const inputPrompts = [];
let questionNumber = 0;
let score = 0;
const questionData = [
{
question: "The HTML5 standard was published in 2014.",
correct_answer: "True",
},
{
question: "The first computer bug was formed by faulty wires.",
correct_answer: "False",
},
{
question: "FLAC stands for 'Free Lossless Audio Condenser'.",
correct_answer: "False",
},
{
question:
"All program codes have to be compiled into an executable file in order to be run. This file can then be executed on any machine.",
correct_answer: "False",
},
{
question: "Linus Torvalds created Linux and Git.",
correct_answer: "True",
},
{
question:
"The programming language 'Python' is based off a modified version of 'JavaScript'",
correct_answer: "False",
},
{
question: "AMD created the first consumer 64-bit processor.",
correct_answer: "True",
},
{
question: "'HTML' stands for Hypertext Markup Language.",
correct_answer: "True",
},
{
question:
"In most programming languages, the operator ++ is equivalent to the statement '+= 1'.",
correct_answer: "True",
},
{
question:
"The IBM PC used an Intel 8008 microprocessor clocked at 4.77 MHz and 8 kilobytes of memory.",
correct_answer: "False",
},
];
//Click Run
$(document).ready(function () {
$("#run-button").click(function () {
inputValues = [];
$("#Content").empty();
restart();
});
});
function restart() {
questionNumber = 0;
score = 0;
nextQuestion();
}
function nextQuestion() {
const currentQuestion = questionData[questionNumber];
questionNumber += 1;
NewLine(
`Q.${questionNumber}: ${currentQuestion.question} (True/False): `,
true
);
}
function checkAnswer(userAnswer, correctAnswer) {
if (userAnswer.toLowerCase() === correctAnswer.toLowerCase()) {
score += 1;
NewLine("You got it right!", false);
} else {
NewLine("That's wrong.", false);
}
NewLine(`The correct answer was: ${correctAnswer}.`, false);
NewLine(`Your current score is: ${score}/${questionNumber}`, false);
NewLine("", false);
NewLine("", false);
}
//Enter button
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
if (x === 13 || x == 13) {
var consoleLine = $("#" + CurrentId + " input").val();
inputValues.push({ id: CurrentId, val: consoleLine });
const userAnswer = inputValues[inputValues.length - 1].val;
const correctAnswer = questionData[questionNumber - 1].correct_answer;
checkAnswer(userAnswer, correctAnswer);
if (questionNumber < questionData.length) {
nextQuestion();
} else {
NewLine("You've completed the quiz", false);
NewLine(`Your final score was: ${score}/${questionNumber}`, false);
NewLine("", false);
NewLine("Process finished with exit code 0 (refresh the page to run the quiz again)", false);
$(".console-carrot").remove();
}
}
});
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
var line = $("#" + CurrentId + " input");
var length = line.val().length;
if (x != 8) {
line.attr("size", 1 + length);
} else {
line.attr("size", length * 0.95);
}
if (length === 0) {
$("#" + CurrentId + " input").attr("size", "1");
}
});
$(document).on("click", function (e) {
$("#" + CurrentId + " input").focus();
});
//New line
function NewLine(text, isPrompt) {
$(".console-carrot").remove();
if (CurrentId !== undefined) {
$("#" + CurrentId + " input").prop("disabled", true);
}
CurrentId = "consoleInput-" + GenerateId();
if (isPrompt) {
$("#Content").append(
//One Line
'<div id="' +
CurrentId +
'">' +
text +
'<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" type="text" class="terminal-input" /><div class="console-carrot"></div></div>'
);
$("#" + CurrentId + " input").focus();
$("#" + CurrentId + " input").attr("size", "1");
} else {
$("#Content").append('<div id="' + CurrentId + '">' + text + "</div>");
}
document.getElementById(CurrentId).scrollIntoView();
}
function GenerateId() {
return Math.random().toString(16).slice(2);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}