Skip to content

Commit f1cd001

Browse files
author
Pretty Taruvinga
committed
predicted and explained the error and corrected the new code to fix the problem
1 parent d093f6d commit f1cd001

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function capitalise(str) {
1414
// The error occurs because we are trying to declare a new variable 'str' inside the function, which is already declared as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same scope. To fix this error, we can simply remove the 'let' keyword and assign the new value to the existing parameter 'str' instead of trying to redeclare it.
1515

1616
function capitalise(str) {
17-
// =============> write your new code here
1817
str = `${str[0].toUpperCase()}${str.slice(1)}`;
1918
return str;
2019
}

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
5+
// I predict that the error is occurring because there is a variable name conflict.
6+
// The parameter 'decimalNumber' is being redeclared inside the function, which is not allowed in JavaScript.
7+
// This will cause a syntax error because we cannot declare a variable with the same name as a parameter within the same scope.
58

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

811
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
12+
const decimalNumber = 0.5;
1013
const percentage = `${decimalNumber * 100}%`;
11-
1214
return percentage;
1315
}
1416

1517
console.log(decimalNumber);
1618

1719
// =============> write your explanation here
20+
// decimalNumber is a parameter of the function convertToPercentage.
21+
// Inside the function, we are trying to declare a new variable with the same
22+
// name 'decimalNumber' using the 'const' keyword.
23+
// This creates a conflict because we cannot have two variables with the same name in the same scope.
24+
// To fix this error, we can simply remove the 'const' keyword and assign the
25+
// new value to the existing parameter 'decimalNumber' instead of trying to redeclare it.
1826

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

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

Lines changed: 10 additions & 2 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+
// I predict that error will occur because the parameter 'num' is not defined in the function.
78

89
function square(3) {
10+
911
return num * num;
1012
}
1113

1214
// =============> write the error message here
13-
15+
// Uncaught SyntaxError: Unexpected number
16+
// =============> explain this error message here
17+
// The error occurs because we are trying to use a number '3' as a parameter in the function declaration, which is not valid syntax in JavaScript.
18+
// Parameters should be variable names, not literal values. To fix this error, we can replace '3' with a valid variable name like 'num'.
1419
// =============> explain this error message here
20+
// To fix this error, we can change the parameter from '3' to a valid variable name like 'num'. This way, we can pass any number as an argument when calling the function, and it will correctly return the square of that number.
1521

1622
// Finally, correct the code to fix the problem
1723

1824
// =============> write your new code here
19-
25+
function square(num) {
26+
return num * num;
27+
}
2028

0 commit comments

Comments
 (0)