Skip to content

Commit 47f18bb

Browse files
committed
Complete 1-key-errors
1 parent 8f3d6cf commit 47f18bb

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// The function is supposed to convert the first character of a string to uppercase
3+
// There will be a syntax error due to the redeclaration of the variable 'str'
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
7+
capitalise("hello");
8+
9+
// function capitalise(str) {
10+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
// return str;
12+
// }
13+
14+
// The error occurred because 'str' was declared again with 'let', which is not allowed.
615

716
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
17+
return `${str[0].toUpperCase()}${str.slice(1)}`;
1018
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
// Predict and explain first...
2+
// The function is intended to take a decimal number and convert it to a percentage (e.g. 1.5 => 150%)
23

34
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5+
// There will be a syntax error due to the redeclaration of the variable 'decimalNumber'
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
9+
// function convertToPercentage(decimalNumber) {
10+
// const decimalNumber = 0.5;
11+
// const percentage = `${decimalNumber * 100}%`;
1112

12-
return percentage;
13-
}
13+
// return percentage;
14+
// }
1415

15-
console.log(decimalNumber);
16+
// console.log(decimalNumber);
1617

17-
// =============> write your explanation here
18+
// Redeclaring the variable decimalNumber causes a syntax error
19+
// The parameter decimalNumber cannot be logged to the console as it is a parameter and holds no value until the function is called
1820

1921
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
22+
function convertToPercentage(decimalNumber) {
23+
return `${decimalNumber * 100}%`;
24+
}
25+
26+
console.log(convertToPercentage(0.5));

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
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

6-
// =============> write your prediction of the error here
5+
// A number is not a valid parameter (Syntax Error)
76

8-
function square(3) {
9-
return num * num;
10-
}
7+
// function square(3) {
8+
// return num * num;
9+
// }
1110

12-
// =============> write the error message here
11+
// Failed to instrument Sprint-2/1-key-errors/2.js
12+
// 7 |
13+
// > 8 | function square(3) {
14+
// | ^ SyntaxError: Unexpected token (8:16)
15+
// 9 | return num * num;
16+
// 10 | }
17+
// 11 |
18+
// at <rootDir>/Sprint-2/1-key-errors/2.js:8:16
1319

14-
// =============> explain this error message here
20+
// parameter names cannot start with a digit
1521

1622
// Finally, correct the code to fix the problem
1723

18-
// =============> write your new code here
19-
24+
function square(num) {
25+
return num * num;
26+
}
2027

28+
console.log(square(3));

0 commit comments

Comments
 (0)