Skip to content

Commit 6852f60

Browse files
committedSep 19, 2019
add 5 new questions and reduce the code by 33%
1 parent 0ac6038 commit 6852f60

File tree

2 files changed

+103
-130
lines changed

2 files changed

+103
-130
lines changed
 

‎src/routes/data/questions.js

Lines changed: 99 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,8 @@ export default [
1111
1212
sayHi();
1313
`,
14-
selections: [
15-
{
16-
des: 'Lydia & undefined',
17-
correct: 0,
18-
},
19-
{
20-
des: 'Lydia & ReferenceError',
21-
correct: 0,
22-
},
23-
{
24-
des: 'ReferenceError & 21',
25-
correct: 0,
26-
},
27-
{
28-
des: 'undefined & ReferenceError',
29-
correct: 1,
30-
},
31-
],
14+
selections: ['Lydia & undefined', 'Lydia & ReferenceError', 'ReferenceError & 21', 'undefined & ReferenceError'],
15+
correct: 3,
3216
explanation: {
3317
html: `
3418
<p>Within the function, we first declare the <b>name</b> variable with the <b>var</b> keyword. This means that the variable gets hoisted (memory space is set up during the creation phase) with the default value of <b>undefined</b>, until we actually get to the line where we define the variable. We haven't defined the variable yet on the line where we try to log the <b>name</b> variable, so it still holds the value of <b>undefined</b>.</p>
@@ -42,24 +26,8 @@ export default [
4226
{
4327
question: `What's the output?`,
4428
code: `['1', '7', '11'].map(parseInt);`,
45-
selections: [
46-
{
47-
des: '[1, 7, 11]',
48-
correct: 0,
49-
},
50-
{
51-
des: '[NaN, NaN, NaN]',
52-
correct: 0,
53-
},
54-
{
55-
des: '[1, NaN, 3]',
56-
correct: 1,
57-
},
58-
{
59-
des: '[1, NaN, 4]',
60-
correct: 0,
61-
},
62-
],
29+
selections: ['[1, 7, 11]', '[NaN, NaN, NaN]', '[1, NaN, 3]', '[1, NaN, 4]'],
30+
correct: 2,
6331
explanation: {
6432
html: `
6533
<p><code>[1, 2, 3, 4, 5].map(console.log);</code> is equivalent to <code>[1, 2, 3, 4, 5].map((val, index, array) => console.log(val, index, array));</code></p>
@@ -85,24 +53,8 @@ export default [
8553
console.log(shape.diameter());
8654
console.log(shape.perimeter());
8755
`,
88-
selections: [
89-
{
90-
des: '<code>20</code> and <code>62.83185307179586</code>',
91-
correct: 0,
92-
},
93-
{
94-
des: '<code>20</code> and <code>NaN</code>',
95-
correct: 1,
96-
},
97-
{
98-
des: '<code>20</code> and <code>63</code>',
99-
correct: 0,
100-
},
101-
{
102-
des: '<code>NaN</code> and <code>63</code>',
103-
correct: 0,
104-
},
105-
],
56+
selections: ['<code>20</code> and <code>62.83185307179586</code>', '<code>20</code> and <code>NaN</code>', '<code>20</code> and <code>63</code>', '<code>NaN</code> and <code>63</code>'],
57+
correct: 1,
10658
explanation: {
10759
html: `
10860
<p>Note that the value of <code>diameter</code> is a regular function, whereas the value of <code>perimeter</code> is an arrow function.</p>
@@ -125,24 +77,8 @@ export default [
12577
setTimeout(() => console.log(i), 1);
12678
}
12779
`,
128-
selections: [
129-
{
130-
des: '<code>0 1 2</code> & <code>0 1 2</code>',
131-
correct: 0,
132-
},
133-
{
134-
des: '<code>0 1 2</code> & <code>3 3 3</code>',
135-
correct: 0,
136-
},
137-
{
138-
des: '<code>3 3 3</code> & <code>0 1 2</code>',
139-
correct: 1,
140-
},
141-
{
142-
des: '<code>3 3 3</code> & <code>0 1 2</code>',
143-
correct: 1,
144-
},
145-
],
80+
selections: ['<code>0 1 2</code> & <code>0 1 2</code>', '<code>0 1 2</code> & <code>3 3 3</code>', '<code>3 3 3</code> & <code>0 1 2</code>', '<code>3 3 3</code> & <code>3 3 3</code>'],
81+
correct: 2,
14682
explanation: {
14783
html: `
14884
<p>Because of the event queue in JavaScript, the <code>setTimeout</code> callback function is called <em>after</em> the loop has been executed. Since the variable <code>i</code> in the first loop was declared using the <code>var</code> keyword, this value was global. During the loop, we incremented the value of <code>i</code> by <code>1</code> each time, using the unary operator <code>++</code>. By the time the <code>setTimeout</code> callback function was invoked, <code>i</code> was equal to <code>3</code> in the first example.</p>
@@ -185,24 +121,8 @@ export default [
185121
}
186122
};
187123
`,
188-
selections: [
189-
{
190-
des: '0 0 2 2',
191-
correct: 0,
192-
},
193-
{
194-
des: '1 1 2 2',
195-
correct: 0,
196-
},
197-
{
198-
des: '0 1 2 3',
199-
correct: 0,
200-
},
201-
{
202-
des: '0 0 2 3',
203-
correct: 1,
204-
},
205-
],
124+
selections: ['0 0 2 2', '1 1 2 2', '0 1 2 3', '0 0 2 3'],
125+
correct: 3,
206126
explanation: {
207127
html: `
208128
<p>1. 第一次和第二次都是在 react 自身生命周期内,触发时 isBatchingUpdates 为 true,所以并不会直接执行更新 state,而是加入了 dirtyComponents,所以打印时获取的都是更新前的状态 0。</p>
@@ -230,24 +150,8 @@ export default [
230150
foo();
231151
boo();
232152
`,
233-
selections: [
234-
{
235-
des: '10 & 20',
236-
correct: 0,
237-
},
238-
{
239-
des: '10 & 10',
240-
correct: 0,
241-
},
242-
{
243-
des: 'undefined & 20',
244-
correct: 0,
245-
},
246-
{
247-
des: 'undefined & ReferenceError',
248-
correct: 1,
249-
},
250-
],
153+
selections: ['10 & 20', '10 & 10', 'undefined & 20', 'undefined & ReferenceError'],
154+
correct: 3,
251155
explanation: {
252156
html: `
253157
<p>The variables declared with var keywords are <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer">hoisted</a> in JavaScript and are assigned a value of <em>undefined</em> in the memory. But initialization happens exactly where you typed them in your code. Also, <em>var-declared</em> variables are <a href="http://2ality.com/2011/02/javascript-variable-scoping-and-its.html" rel="noopener noreferrer">function-scoped</a>, whereas <em>let</em> and <strong>const</strong> have block-scoped. So, this is how the process will look like:</p>
@@ -312,24 +216,8 @@ export default [
312216
313217
console.log('8');
314218
`,
315-
selections: [
316-
{
317-
des: '4 1 3 6 8 2 7 5',
318-
correct: 1,
319-
},
320-
{
321-
des: '4 1 3 2 6 7 8 5',
322-
correct: 0,
323-
},
324-
{
325-
des: '4 1 3 2 5 6 7 8',
326-
correct: 0,
327-
},
328-
{
329-
des: '4 5 1 3 2 6 7 8',
330-
correct: 0,
331-
},
332-
],
219+
selections: ['4 1 3 6 8 2 7 5', '4 1 3 2 6 7 8 5', '4 1 3 2 5 6 7 8', '4 5 1 3 2 6 7 8'],
220+
correct: 0,
333221
explanation: {
334222
html: `
335223
<iframe width="560" height="315" src="https://www.youtube.com/embed/8aGhZQkoFbQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
@@ -340,4 +228,89 @@ export default [
340228
status: 0,
341229
selected: undefined,
342230
},
231+
{
232+
question: `What's the output?`,
233+
code: `
234+
'💩'.length;
235+
`,
236+
selections: ['undefined', '1', '2', 'Error'],
237+
correct: 2,
238+
explanation: {
239+
html: `
240+
String.prototype.length returns the number of bytes rather than the number of characters. Unicode characters like emoji, require two bytes.
241+
`,
242+
origin: 'https://frontendmasters.com/courses/debugging-javascript/common-javascript-quirks/',
243+
},
244+
keywords: ['String.prototype.length'],
245+
status: 0,
246+
selected: undefined,
247+
},
248+
{
249+
question: `What's the output?`,
250+
code: `
251+
0.1 + 0.2
252+
`,
253+
selections: ['0.3', '0.2999999999997', '0.3000000000004', '\'0.3\''],
254+
correct: 2,
255+
explanation: {
256+
html: `
257+
Go checkout <a href="https://0.30000000000000004.com" target="_blank">0.30000000000000004</a>
258+
`,
259+
origin: 'https://frontendmasters.com/courses/debugging-javascript/common-javascript-quirks/',
260+
},
261+
keywords: ['floating point', '0.30000000000000004'],
262+
status: 0,
263+
selected: undefined,
264+
},
265+
{
266+
question: `What's the output?`,
267+
code: `
268+
new Date(2019, 5, 31);
269+
`,
270+
selections: ['2019 July 1', '2019 Jun 31', '2019 May 31', '2020 May 31'],
271+
correct: 0,
272+
explanation: {
273+
html: `
274+
Months are zero based in Date. This specifies June 31, which overflows to July 1.
275+
`,
276+
origin: 'https://frontendmasters.com/courses/debugging-javascript/common-javascript-quirks/',
277+
},
278+
keywords: ['date'],
279+
status: 0,
280+
selected: undefined,
281+
},
282+
{
283+
question: `What's the output?`,
284+
code: `
285+
new Array(0, 1, Array(2));
286+
`,
287+
selections: ['[0, 1, [2]]', '[0, 1, 2]', '[0, 1, [object Array]]', '[0, 1, [undefined, undefined]]'],
288+
correct: 3,
289+
explanation: {
290+
html: `
291+
Instantiating an Array with multiple arguments creates an Array from those values. However, a single argument only specifies the length.
292+
`,
293+
origin: 'https://frontendmasters.com/courses/debugging-javascript/common-javascript-quirks/',
294+
},
295+
keywords: ['Array'],
296+
status: 0,
297+
selected: undefined,
298+
},
299+
{
300+
question: `What's the output?`,
301+
code: `
302+
[10, 5, 1].sort();
303+
`,
304+
selections: ['[1, 5, 10]', '[10, 5, 1]', '[1, 10, 5]', '[5, 10, 1]'],
305+
correct: 2,
306+
explanation: {
307+
html: `
308+
Array.prototype.sort's default comparator assumes String operation. All values are coerced and compared as Strings.
309+
`,
310+
origin: 'https://frontendmasters.com/courses/debugging-javascript/common-javascript-quirks/',
311+
},
312+
keywords: ['Array'],
313+
status: 0,
314+
selected: undefined,
315+
},
343316
];

‎src/routes/index.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
4545
$: code = questions.length > 0 ? Prism.highlight(nw.normalize(questions[current].code), Prism.languages.javascript, 'javascript') : undefined;
4646
47-
const handleOnSelection = (correct, index) => {
47+
const handleOnSelection = index => {
4848
questions[current].selected = selected = index;
49-
questions[current].selections[index].correct ? statistics.correct = [...statistics.correct, current] : statistics.wrong = [...statistics.wrong, current];
49+
questions[current].correct === index ? statistics.correct = [...statistics.correct, current] : statistics.wrong = [...statistics.wrong, current];
5050
statistics.last = current;
5151
localStorage.setItem('questions', JSON.stringify(questions));
5252
localStorage.setItem('statistics', JSON.stringify(statistics));
@@ -443,8 +443,8 @@
443443
</pre>
444444
<div class="selections {questions[current].selected > -1 ? 'selected' : ''}">
445445
{#each questions[current].selections as selection, index}
446-
<div disabled class="selection d-flex justify-between align-center {selected === index ? 'selected' : ''} {selected === index && questions[current].selections[index].correct ? 'right' : 'wrong'}" on:click={() => handleOnSelection(selection.correct, index)}>
447-
<span>{@html selection.des}</span>
446+
<div disabled class="selection d-flex justify-between align-center {selected === index ? 'selected' : ''} {selected === index && questions[current].correct === index ? 'right' : 'wrong'}" on:click={() => handleOnSelection(index)}>
447+
<span>{@html selection}</span>
448448
<div class="check"></div>
449449
</div>
450450
{/each}

0 commit comments

Comments
 (0)
Please sign in to comment.