|
7 | 7 | <link rel="stylesheet" href="checker.css"> |
8 | 8 | <script src="js-yaml.min.js"></script> |
9 | 9 | <script src="checker.js"></script> |
| 10 | +<script src="deserialization.js"></script> |
10 | 11 | <link rel="license" href="https://creativecommons.org/licenses/by/4.0/"> |
11 | 12 |
|
12 | 13 | <!-- See create_labs.md for how to create your own lab! --> |
13 | 14 |
|
14 | | -<!-- Sample expected answer --> |
15 | | -<script id="expected0" type="plain/text"> |
16 | | - const data = JSON.parse(base64Decoded); |
17 | | -</script> |
18 | | -<!-- |
19 | | ---> |
20 | | -<script id="expected1" type="plain/text"> |
21 | | - if (data.username && typeof data.username == 'string' && data.username.length < 20) { |
22 | | -</script> |
23 | | - |
24 | | -<!-- Full pattern of correct answer --> |
25 | | -<script id="correct0" type="plain/text"> |
26 | | -\s* const data = JSON \. parse \( base64Decoded \) \; \s* |
27 | | -</script> |
28 | | -<script id="correct1" type="plain/text"> |
29 | | -\s* if \( CONDALL \) \{ \s* |
30 | | -</script> |
31 | | - |
32 | | -<script id="info" type="application/yaml"> |
33 | | ---- |
34 | | -# Allow condition subexpressions to be optionally surrounded by parentheses |
35 | | -# and allow the order to vary. This allows more real-world answers to be |
36 | | -# considered acceptable. |
37 | | -# Doing this is more easily done by buildigg up definitions, |
38 | | -# which is annoying to write but general. |
39 | | -definitions: |
40 | | -- term: COND0 |
41 | | - value: |- |
42 | | - data \. username |
43 | | -- term: COND0 |
44 | | - value: |- |
45 | | - (COND0|\( COND0 \)) |
46 | | -- term: COND1 |
47 | | - value: |- |
48 | | - typeof\s+data \. username == ('string'|"string"|`string`) |
49 | | -- term: COND1 |
50 | | - value: |- |
51 | | - (COND1|\( COND1 \)) |
52 | | -- term: COND2 |
53 | | - value: |- |
54 | | - data \. username \. length < 20 |
55 | | -- term: COND2 |
56 | | - value: |- |
57 | | - (COND2|\( COND2 \)) |
58 | | -# Only the first one is likely, but we may as well allow both possibilities. |
59 | | -# The first condition MUST be first because it checks if the value exists. |
60 | | -- term: CONDALL |
61 | | - value: |- |
62 | | - (COND0 && (COND1 && COND2|COND2 && COND1)) |
63 | | -hints: |
64 | | -- absent: | |
65 | | - ^ const data = |
66 | | - text: The first section should begin with `const data =` |
67 | | -- present: "json" |
68 | | - text: the JSON built-in global object is witten in uppercase. |
69 | | -- absent: | |
70 | | - JSON \. parse |
71 | | - text: Make a call to `JSON.parse` with the data retrieved, e.g., |
72 | | - `JSON.parse(base64Decoded)` should be stored in `data`. |
73 | | -- present: | |
74 | | - \+ |
75 | | - text: You should not have any concatenation (`+`) in the first section. |
76 | | -- absent: | |
77 | | - ; $ |
78 | | - text: JavaScript does not require semicolons at the end of a |
79 | | - statement, but since the other statements terminate with semicolons, |
80 | | - you should also terminate your statement with a semicolon to be consistent. |
81 | | -- absent: |- |
82 | | - ^ if \( |
83 | | - index: 1 |
84 | | - text: The second section should start with `if (` followed by a condition. |
85 | | - examples: |
86 | | - - |
87 | | - - const data = JSON.parse(base64Decoded); |
88 | | - - | |
89 | | - if data.username { |
90 | | -- absent: | |
91 | | - data \. username |
92 | | - index: 1 |
93 | | - text: Check if the data object has a property called username. You can do this by referencing data.username. |
94 | | -- absent: \&\& |
95 | | - index: 1 |
96 | | - text: To combine multiple conditions in JavaScript use &&. This operator means 'and', so both conditions must be true for the entire statement to pass. |
97 | | -- absent: typeof |
98 | | - index: 1 |
99 | | - text: Use typeof to check the type of the operand's value. |
100 | | - You should have `typeof data.username == 'string'` |
101 | | - or similar. |
102 | | -- present: | |
103 | | - typeof data \. username == 'String' |
104 | | - index: 1 |
105 | | - text: When using typeof, JavaScript expects "string" all lowercase. |
106 | | -- absent: length |
107 | | - index: 1 |
108 | | - text: check if the length of the string is smaller than 20 characters. |
109 | | - Use the expression `data.username.length < 20` to determine this. |
110 | | -- present: |- |
111 | | - ^ if \( |
112 | | - absent: |- |
113 | | - ^ if \( data \. username && |
114 | | - index: 1 |
115 | | - text: Begin the second section with `if ( data.username && ... ` |
116 | | - because you must check if data is even present before you can check |
117 | | - various attributes of that data. |
118 | | - examples: |
119 | | - - |
120 | | - - "const data = JSON.parse(base64Decoded);" |
121 | | - - "if (typeof data.username == 'string' && data.username.length < 20 && data.username) {" |
122 | | -successes: |
123 | | - - |
124 | | - - const data = JSON.parse(base64Decoded); |
125 | | - - if (data.username && typeof data.username == 'string' && data.username.length < 20) { |
126 | | - - |
127 | | - - const data = JSON . parse ( base64Decoded ) ; |
128 | | - - if ( data . username && typeof data . username == 'string' && data . username.length < 20) { |
129 | | - - |
130 | | - - const data = JSON.parse(base64Decoded); |
131 | | - - if (data.username && (typeof data.username == 'string') && (data.username.length < 20)) { |
132 | | - - |
133 | | - - const data = JSON.parse(base64Decoded); |
134 | | - - if (data.username && typeof data.username == 'string' && (data.username.length < 20)) { |
135 | | -failures: |
136 | | - - |
137 | | - - const data = JSON.parse(base64Decoded); |
138 | | - - if (data.username && (typeof data.username == 'string')) && (data.username.length < 20)) { |
139 | | -# debug: true |
140 | | -</script> |
141 | 15 | </head> |
142 | 16 | <body> |
143 | 17 | <!-- For GitHub Pages formatting: --> |
|
0 commit comments