Skip to content

Commit 1ff607f

Browse files
committed
Key-errors tasks are done
1 parent 7ed381b commit 1ff607f

File tree

7 files changed

+4460
-21
lines changed

7 files changed

+4460
-21
lines changed

Module-Structuring-and-Testing-Data.test.js

Whitespace-only changes.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
let age =33;
3+
let age = 33;
44
age = age + 1;
5-
console.log(age);
5+
console.log(age);

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> Prediction:
3+
// Calling capitalise("hello") will cause an error:
4+
// "Identifier 'str' has already been declared"
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
@@ -9,5 +11,15 @@ function capitalise(str) {
911
return str;
1012
}
1113

12-
// =============> write your explanation here
13-
// =============> write your new code here
14+
// =============> Explanation:
15+
// The error happens because 'str' is already a parameter.
16+
// We cannot declare another variable with the same name inside the function.
17+
18+
// =============> Corrected Code:
19+
function capitalise(str) {
20+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
21+
return str;
22+
}
23+
24+
// Example
25+
console.log(capitalise("hello")); // Output: "Hello"

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
6-
// Try playing computer with the example to work out what is going on
4+
// =============> Prediction:
5+
// There will be an error because 'decimalNumber' is declared twice inside the function
6+
// and console.log(decimalNumber) outside the function will not work.
77

8+
// Original code:
89
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10+
const decimalNumber = 0.5; // cannot declare again
1011
const percentage = `${decimalNumber * 100}%`;
11-
1212
return percentage;
1313
}
1414

15-
console.log(decimalNumber);
15+
console.log(decimalNumber); // not defined
16+
17+
// =============> Explanation:
18+
// 1. The function already has a parameter called 'decimalNumber', so using 'const decimalNumber' again is not allowed.
19+
// 2. The variable 'decimalNumber' only exists inside the function.
20+
// You cannot use it outside the function.
1621

17-
// =============> write your explanation here
22+
// =============> Corrected Code:
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = decimalNumber * 100 + "%"; // calculate percentage
25+
return percentage;
26+
}
1827

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
28+
// Example
29+
console.log(convertToPercentage(0.5)); // Output: "50%"

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33

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

6-
// =============> write your prediction of the error here
6+
// =============> Prediction of the error:
7+
// The function will give an error because '3' is not a valid variable name.
8+
// Function parameters must have names, not numbers.
79

810
function square(3) {
911
return num * num;
1012
}
1113

12-
// =============> write the error message here
14+
// =============> Error message:
15+
// SyntaxError: Unexpected number
1316

14-
// =============> explain this error message here
17+
// =============> Explanation of the error:
18+
// 1. You cannot use a number as a parameter name.
19+
// 2. Also, 'num' is not defined inside the function, so it will cause another error if we fix the parameter first.
1520

1621
// Finally, correct the code to fix the problem
1722

18-
// =============> write your new code here
23+
// =============> Corrected Code:
24+
function square(num) {
25+
return num
26+
}
1927

2028

0 commit comments

Comments
 (0)