You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-thrownewError("Division by zero is not allowed");
156
-
-returna/b;
157
-
-" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result.message);"
158
-
-
159
-
-thrownewError("Division by zero is not allowed");
160
-
-returna/b;
161
-
-" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result );"
162
-
# debug: true
163
-
</script>
164
15
</head>
165
16
<body>
166
17
<!-- For GitHub Pages formatting: -->
@@ -199,7 +50,7 @@ <h2>Task Information</h2>
199
50
<ol>
200
51
<li>Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is <tt>divide</tt>.</li>
201
52
<li>Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter <tt>b</tt> is zero.</li>
202
-
<li>Set the error message to "Division by zero is not allowed".</li>
53
+
<li>Set the error message to "Division by zero is forbidden".</li>
203
54
<li>Update the success path to return the result of the division operation.</li>
204
55
<li>Modify the calling code to use a try block to wrap the call to the <tt>divide</tt> function.</li>
205
56
<li>Within the try block, log the result of the division operation if no error is thrown.</li>
text: "This code uses the convention of terminating each line with a semicolon; please follow the conventions of the code being modified.\n"
8
+
},
9
+
{
10
+
index: 0,
11
+
present: "(Throw|THROW|New|NEW|error|ERROR)",
12
+
text: "JavaScript is case-sensitive. use throw new Error(...).",
13
+
examples: [
14
+
[" Throw new Error(\"Division by zero is forbidden\");"],
15
+
[" THROW new Error(\"Division by zero is forbidden\");"],
16
+
[" throw New Error(\"Division by zero is forbidden\");"],
17
+
[" throw NEW Error(\"Division by zero is forbidden\");"],
18
+
[" throw new error(\"Division by zero is forbidden\");"],
19
+
[" throw new ERROR(\"Division by zero is forbidden\");"]
20
+
]
21
+
},
22
+
{
23
+
index: 0,
24
+
absent: "throw",
25
+
text: "Try using the throw keyword to raise an exception, E.g., throw new Error(\"Message\").",
26
+
examples: [
27
+
[" return { success: false, message: \"Division by zero is forbidden\" };"],
28
+
[" return \"Division by zero is forbidden\" ;"]
29
+
]
30
+
},
31
+
{
32
+
index: 1,
33
+
absent: "return",
34
+
text: "Use the return keyword to return the result of the division.",
35
+
examples: [
36
+
[" a / b ;"]
37
+
]
38
+
},
39
+
{
40
+
index: 1,
41
+
present: "{ (.*?)} ",
42
+
text: "Try simply returning the result of the division.",
43
+
examples: [
44
+
[
45
+
"throw new Error(\"Division by zero is forbidden\");",
46
+
" return { success: true, result: a / b };"
47
+
],
48
+
[
49
+
"throw new Error(\"Division by zero is forbidden\");",
50
+
" return { result: a / b };"
51
+
]
52
+
]
53
+
},
54
+
{
55
+
index: 2,
56
+
absent: String.raw`\s*try\s*{\s* `,
57
+
text: "Use a try block to catch any exceptions that might be thrown. It should look something like `try { ... } catch(err) {...}` (fill in the `...` sections).",
58
+
examples: [
59
+
[
60
+
"throw new Error(\"Division by zero is forbidden\");",
text: "When reporting the error, you need to report it in the catch block, which catches it as the variable `err`. Thus, you need to use `err.message` not `result` or `result.message`, since the error is in `err.message`. Note that the variable `result` is out of scope in the catch block anyway; it was declared in the try block.",
130
+
examples: [
131
+
[
132
+
"throw new Error(\"Division by zero is forbidden\");",
133
+
"return a / b;",
134
+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result.message);"
135
+
],
136
+
[
137
+
"throw new Error(\"Division by zero is forbidden\");",
138
+
"return a / b;",
139
+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result );"
140
+
]
141
+
]
142
+
}
143
+
],
144
+
expected: [
145
+
'throw new Error("Division by zero is forbidden");',
146
+
'return a / b;',
147
+
`try {
148
+
const result = divide(10, 2);
149
+
console.log("Result:", result);
150
+
} catch (err) {
151
+
console.error("Error:", err.message);
152
+
}`
153
+
],
154
+
correct: [
155
+
String.raw`\s* throw new Error \( ("(.*)"|'(.*)'|${BACKQUOTE}(.*)${BACKQUOTE}) \) ; \s*`,
156
+
String.raw`\s* return a \/ b ; \s*`,
157
+
String.raw`\s* try \{
158
+
const result = divide \( 10 , 2 \) ;
159
+
console \. log \(
160
+
("Result:" | 'Result:' | ${BACKQUOTE}Result:${BACKQUOTE}) , result \) ;
0 commit comments