Skip to content

Commit 482253a

Browse files
committed
key errors done
1 parent 1a1b90b commit 482253a

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// the str variable declaration inside of the function shadows the function parameter
4+
// if an empty string is passed to the function, trying to access element at index 0, and calling
5+
// toUpperCase() on it will cause an error, as that value would be undefined.
36

47
// call the function capitalise with a string input
58
// interpret the error message and figure out why an error is occurring
@@ -10,4 +13,12 @@ function capitalise(str) {
1013
}
1114

1215
// =============> write your explanation here
16+
// Error message is:
17+
//SyntaxError: Identifier 'str' has already been declared
18+
// it means that JavaScript has already declared a variable name 'str' within the function's scope.
19+
// so when you try to declare it in the body of the function, there's a naming conflict.
20+
1321
// =============> write your new code here
22+
function capitalise2(str) {
23+
return `${str[0].toUpperCase()}${str.slice(1)}`;
24+
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// First, it's trying to print decimalNumber, an undeclared value.
6+
// Second, the decimalNumber variable name is already taken when it tries to declare it in the first
7+
// line of the function body.
58

69
// Try playing computer with the example to work out what is going on
710

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

12-
return percentage;
13-
}
15+
// return percentage;
16+
// }
1417

15-
console.log(decimalNumber);
18+
// console.log(decimalNumber);
1619

1720
// =============> write your explanation here
21+
// when Javascript reads the function definition, and sees a parameter, it creates a variable with that name
22+
// scoped to the body of the function. Then when it reads the first line within the body, the 'decimalNumber'
23+
// name is already taken.
1824

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

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
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
6+
// the function parameter cannot be a number, or start with a number, follows same rules as variable names
7+
// num is undefined.
78

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

1213
// =============> write the error message here
14+
//SyntaxError: Unexpected number
1315

1416
// =============> explain this error message here
17+
// function parameter is a number, which is not a valid type for parameters
1518

1619
// Finally, correct the code to fix the problem
1720

18-
// =============> write your new code here
19-
20-
21+
function square(num) {
22+
return num * num;
23+
}

0 commit comments

Comments
 (0)