Skip to content

Commit c26d1a3

Browse files
committed
refactor: fix syntax errors and improve function definitions in key error examples
1 parent f33255c commit c26d1a3

File tree

3 files changed

+9
-37
lines changed

3 files changed

+9
-37
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,11 @@
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-
*/
12-
// =============> 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.
1511

12+
// =============> write your explanation here
1613
// =============> 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: 1 addition & 15 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,22 +13,8 @@ function convertToPercentage(decimalNumber) {
1313
}
1414

1515
console.log(decimalNumber);
16-
*/
1716

1817
// =============> write your explanation here
19-
/*
20-
1. Undeclared variables are automatically declared when first used. As a parameter in the 'convertToPercentage' function,
21-
'decimalNumber' is (automatically) already declared. trying to declare it again with 'const' will cause a syntax error.
22-
2. Also, the 'console.log(decimalNumber);' line is outside the function, so 'decimalNumber' is not defined in that scope.
23-
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.
24-
*/
2518

2619
// Finally, correct the code to fix the problem
2720
// =============> write your new code here
28-
function convertToPercentage(decimalNumber) {
29-
const percentage = `${decimalNumber * 100}%`;
30-
31-
return percentage;
32-
}
33-
34-
console.log(convertToPercentage(0.5));

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

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

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

56
// =============> write your prediction of the error here
6-
/*
7+
78
function square(3) {
89
return num * num;
910
}
10-
*/
11+
1112
// =============> write the error message here
12-
// 1-SyntaxError: Unexpected number.
13+
1314
// =============> 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-
*/
15+
1916
// Finally, correct the code to fix the problem
2017

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

0 commit comments

Comments
 (0)