Skip to content

Commit 9659871

Browse files
committed
Fix syntax errors and improve function implementations in key error examples
1 parent c2ed214 commit 9659871

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

Sprint-2/1-key-errors/0.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
6-
6+
/*
77
function capitalise(str) {
88
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
11-
11+
*/
1212
// =============> write your explanation here
13+
/*Undeclared variables are automatically declared when first used. As a parameter in the 'capitalise' function,
14+
'str' is already declared. trying to declare it again with 'let' will cause a syntax error.
15+
*/
1316
// =============> write your new code here
17+
function capitalise(str) {
18+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
19+
return str;
20+
}
21+
console.log(capitalise("hello world")); // This line is inserted only to test the code. It returns "Hello world"

Sprint-2/1-key-errors/1.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// =============> write your prediction here
55

66
// Try playing computer with the example to work out what is going on
7-
7+
/*
88
function convertToPercentage(decimalNumber) {
99
const decimalNumber = 0.5;
1010
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +13,20 @@ function convertToPercentage(decimalNumber) {
1313
}
1414
1515
console.log(decimalNumber);
16-
16+
*/
1717
// =============> write your explanation here
18-
18+
/*
19+
1. Undeclared variables are automatically declared when first used. As a parameter in the 'convertToPercentage' function,
20+
'decimalNumber' is (automatically) already declared. trying to declare it again with 'const' causes a syntax error.
21+
2. Also, the 'console.log(decimalNumber);' line is outside the function, so 'decimalNumber' is not defined in that scope.
22+
3 decimalNumber = 0.5; overwrites the argument supplied, and so to make the function work as intended, this line should be removed, so the function does not ignore its input argument.
23+
*/
1924
// Finally, correct the code to fix the problem
2025
// =============> write your new code here
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${decimalNumber * 100}%`;
28+
29+
return percentage;
30+
}
31+
32+
console.log(convertToPercentage(0.25)); // This line is inserted only to test the code. It returns "25%"

Sprint-2/1-key-errors/2.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

43
// this function should square any number but instead we're going to get an error
54

65
// =============> write your prediction of the error here
7-
6+
/*
87
function square(3) {
98
return num * num;
109
}
11-
10+
*/
1211
// =============> write the error message here
13-
12+
// SyntaxError: Unexpected number
1413
// =============> explain this error message here
14+
/*
15+
1-The parameter name '3' is not a valid identifier. Function parameters names must (be valid variable names & not literal values), i.e. start with a letter, underscore (_), or dollar sign ($), and cannot be a number.
16+
2-Also, the function uses 'num' which is not defined anywhere. It should use the parameter name instead.
17+
3-Finally, the function is not called anywhere, so it won't produce any output.
18+
*/
1519

1620
// Finally, correct the code to fix the problem
1721

1822
// =============> write your new code here
19-
20-
23+
function square(num) {
24+
return num * num;
25+
}
26+
console.log(square(3)); // This line is inserted only to test the function. It returns 9

0 commit comments

Comments
 (0)