Skip to content

Commit 2a0a2b7

Browse files
committed
completed the 1-key-errors of sprint-2
1 parent 3372770 commit 2a0a2b7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

7-
function capitalise(str) {
7+
/* function capitalise(str) {
88
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
11+
*/
1112

1213
// =============> write your explanation here
14+
// The error is occurring because we are trying to declare a variable with the same name as the function parameter 'str'. This causes a conflict and results in a SyntaxError. To fix this, we can simply remove the 'let' keyword and assign the new value to 'str' directly, since 'str' is already defined as a parameter.
15+
16+
1317
// =============> write your new code here
18+
function capitalise(str) {
19+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
20+
return str;
21+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// An error will occur because we are trying to declare a variable with the same name as a function parameter. In this case, we are declaring 'decimalNumber' inside the function, which conflicts with the parameter 'decimalNumber'. This will result in a SyntaxError because we cannot have two variables with the same name in the same scope.
56

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

8-
function convertToPercentage(decimalNumber) {
9+
/*function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
1314
}
1415
1516
console.log(decimalNumber);
17+
*/
1618

1719
// =============> write your explanation here
20+
// The error is occurring because we are trying to declare a variable with the same name as a function parameter. In this case, we are declaring 'decimalNumber' inside the function, which conflicts with the parameter 'decimalNumber'. This will result in a SyntaxError because we cannot have two variables with the same name in the same scope.
1821

1922
// Finally, correct the code to fix the problem
2023
// =============> write your new code here
24+
25+
function convertToPercentage(decimalNumber) {
26+
const percentage = `${decimalNumber * 100}%`;
27+
return percentage;
28+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
// An error will occur because the function is defined with an invalid parameter name '3'. In JavaScript, parameter names must be valid identifiers, which cannot start with a number. This will result in a SyntaxError when the code is parsed.
78

8-
function square(3) {
9+
/* function square(3) {
910
return num * num;
1011
}
12+
*/
1113

1214
// =============> write the error message here
15+
// SyntaxError: Unexpected number '3'. Parameter names must be valid identifiers and cannot start with a number.
1316

1417
// =============> explain this error message here
18+
// The error message indicates that the number '3' is unexpected in the context of a parameter name. In JavaScript, parameter names must be valid identifiers, which means they cannot start with a number. To fix this error, we need to change the parameter name to a valid identifier, such as 'num'.
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(5));
1927

2028

0 commit comments

Comments
 (0)