Skip to content

Commit 6fc10b4

Browse files
committed
Adjustments made to the files in Sprint-2/1-key-errors to fix key errors and improve code functionality. The commit includes modifications to 0.js, 1.js, and 2.js to address variable declaration issues and parameter naming conventions.
1 parent 3372770 commit 6fc10b4

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The command is to capitalise the first letter of the string.
34

45
// call the function capitalise with a string input
5-
// interpret the error message and figure out why an error is occurring
6+
// The code will produce an error before the function runs properly. The error message will say, SyntaxError: Identifier 'str' has already been declared
7+
8+
// interpret the error message and figure out why an error is occurring.
9+
// The error is occurring because the variable str is being declared twice in the function. The first declaration is in the function parameter, and the second declaration is inside the function body. This causes a conflict because you cannot declare a variable with the same name in the same scope.
610

711
function capitalise(str) {
812
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
1014
}
1115

1216
// =============> write your explanation here
17+
// The function capitalise takes a string input and attempts to capitalise the first letter of the string. However, the variable str is declared twice, which causes a SyntaxError. To fix this, we can remove the second declaration of str inside the function body and directly return the capitalised string.
1318
// =============> write your new code here
19+
// The corrected function should look like this:
20+
21+
function capitalise(str) {
22+
return `${str[0].toUpperCase()}${str.slice(1)}`;
23+
}
24+
25+
// Now the function should work correctly and capitalise the first letter of the input string.

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

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

33
// Why will an error occur when this program runs?
4+
// The error occurs because the variable decimalNumber is declared twice in the function convertToPercentage. The first declaration is in the function parameter, and the second declaration is inside the function body. This causes a conflict because you cannot declare a variable with the same name in the same scope.
45
// =============> write your prediction here
6+
// The command is to convert a decimal number to a percentage.
57

68
// Try playing computer with the example to work out what is going on
9+
//
710

811
function convertToPercentage(decimalNumber) {
912
const decimalNumber = 0.5;
@@ -15,6 +18,16 @@ function convertToPercentage(decimalNumber) {
1518
console.log(decimalNumber);
1619

1720
// =============> write your explanation here
21+
// The function convertToPercentage takes a decimal number as input and attempts to convert it to a percentage. However, the variable decimalNumber is declared twice, which causes a SyntaxError. To fix this, we can remove the second declaration of decimalNumber inside the function body and directly return the calculated percentage.
1822

1923
// Finally, correct the code to fix the problem
2024
// =============> write your new code here
25+
// The corrected function should look like this:
26+
27+
function convertToPercentage(decimalNumber) {
28+
const percentage = `${decimalNumber * 100}%`;
29+
30+
return percentage;
31+
}
32+
33+
// Now the function should work correctly and convert a decimal number to a percentage.

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

Lines changed: 8 additions & 0 deletions
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+
// The error occurs because the parameter name '3' is not a valid identifier in JavaScript. Variable and parameter names cannot start with a number. This will cause a SyntaxError when the code is parsed.
78

89
function square(3) {
910
return num * num;
1011
}
1112

1213
// =============> write the error message here
14+
// The error message will be: SyntaxError: Unexpected number '3'. This indicates that the parser encountered an unexpected token, which is the number '3' used as a parameter name.
1315

1416
// =============> explain this error message here
17+
// The error message indicates that the parser is expecting a valid identifier for the parameter name, but it encountered a number instead. In JavaScript, variable and parameter names must start with a letter, underscore, or dollar sign, and cannot start with a number. Therefore, using '3' as a parameter name is not allowed and results in a SyntaxError.
1518

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

1821
// =============> write your new code here
22+
function square(num) {
23+
return num * num;
24+
}
25+
26+
// Now the function should work correctly and return the square of the input number.
1927

2028

0 commit comments

Comments
 (0)