Skip to content

Commit 2467b5f

Browse files
committed
predicted errors explained and fixed
1 parent 3372770 commit 2467b5f

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// //My prediction` => The capitalise function tries to capitalize the first character of the string and concat it with the rest. This function may not run as it has error.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,10 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
// call the function with a string argument
13+
capitalise("hello world");
14+
// the error is occurring because we are trying to declare a variable with the same name as the function parameter
15+
16+
function capitalise(str) {
17+
return `${str[0].toUpperCase()}${str.slice(1)}`;
18+
}

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

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

3+
4+
35
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
6+
// The error will occur because the variable 'decimalNumber' is being redeclared.
57

68
// Try playing computer with the example to work out what is going on
79

@@ -14,7 +16,14 @@ function convertToPercentage(decimalNumber) {
1416

1517
console.log(decimalNumber);
1618

17-
// =============> write your explanation here
19+
// the error is occurring because we are trying to declare a variable with the same name as the function parameter, which is not allowed in JavaScript.
1820

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
//It will throw a syntax error because 3 is not a valid.
34

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

67
// =============> write your prediction of the error here
8+
// this function should square any number but instead we're going to get an error
79

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

1214
// =============> write the error message here
15+
// SyntaxError: Unexpected number
1316

1417
// =============> explain this error message here
18+
//The error occurs because function parameters must be valid identifiers (i.e., variable names), not literal numbers.
1519

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

1822
// =============> write your new code here
23+
function square(num) {
24+
return num * num;
25+
}
26+
console.log(square(3));
1927

2028

0 commit comments

Comments
 (0)