-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
144 lines (119 loc) · 4.35 KB
/
script.js
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
const facts = [
{
"statement": "JavaScript was invented in 1995",
"answer": "true",
"explanation": "Brendan Eich created JS at Netscape in 1995. The initial version of the language was written in just 10 days."
},
{
"statement": "console.log(typeof true) = boolean",
"answer": "true",
"explanation": "typeof true/false is boolean"
},
{
"statement": " console.log(typeof document.title) = boolean ",
"answer": "fasle",
"explanation": "typeof document.title is String because it is title of the webpage"
},
{
"statement": " console.log(typeof null) = object ",
"answer": "true",
"explanation": "typeof null is object. because some historical accident they set as object but actually it is premetive datatype"
},
{
"statement": " if (var v = 42n) then console.log(typeof v) = number ",
"answer": "fasle",
"explanation": "if (var v = 42n) then console.log(typeof v) = bigint. because it is represent value is too large "
},
{
"statement": "console.log(Math.random()) will give any number",
"answer": "false",
"explanation": "console.log(Math.random()) It will give any number between 0 to 1"
},
{
"statement": "Strings in JS are editable values",
"answer": "false",
"explanation": "In JavaScript strings are immutable values, meaning they cannot be edited; however, they can replaced with new, different strings."
},
{
"statement": "1 + 1 === 2",
"answer": "true",
"explanation": "The plus operator gives the sum of two numbers."
},
{
"statement": "'1' + '1' === '2'",
"answer": "false",
"explanation": "The plus operator concatenates (joins together) strings, so '1' + '1' === '11'."
},
{
"statement": "typeof ['J', 'S'] === 'array'",
"answer": "false",
"explanation": "Arrays have the type 'object'. In JS, everything is either a primitive data type (e.g. 'string', 'number') or an object. Arrays are a kind of object with some special properties. "
}
];
function hide(element) {
element.classList.add("hidden");
}
function show(element) {
element.classList.remove("hidden");
}
function disable(button) {
button.setAttribute("disabled", "");
}
function enable(button) {
button.removeAttribute("disabled");
}
let correct = 0;
let completed = 0;
let fact;
const explanation = document.getElementById("explanation");
const nextButton = document.getElementById("next-question");
const optionButtons = document.getElementById("options").children;
function getNextFact() {
fact = facts.shift(); // get the first fact in our array (shortening the array)
// set the question text to the current fact's statement
document.getElementById("statement").textContent = fact.statement;
// hide any previous explanation
hide(explanation);
for (let option of optionButtons) {
// clear any previous classes
option.classList.remove("correct");
option.classList.remove("incorrect");
// make sure buttons are enabled
enable(option);
}
// disable next-question button
disable(nextButton);
}
nextButton.addEventListener("click", getNextFact);
for (let option of optionButtons) {
option.addEventListener("click", e => {
// When this option is clicked...
// disable all the option buttons
for (let button of optionButtons) {
disable(button);
}
// enable the 'next question' button, if we still have facts left
if (facts.length > 0) {
enable(nextButton);
} else {
nextButton.textContent = "No more questions!"
}
const guess = e.target.value;
if (guess === fact.answer) {
// correct answer!
e.target.classList.add("correct");
correct += 1;
} else {
// wrong answer!
e.target.classList.add("incorrect");
}
// display the explanation
explanation.textContent = fact.explanation;
show(explanation);
// update the score
completed += 1;
document.getElementById("correct").textContent = correct;
document.getElementById("completed").textContent = completed;
})
}
getNextFact();