Skip to content

Commit 5729ef6

Browse files
committed
key errors completed
1 parent 0224852 commit 5729ef6

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,32 @@
33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
55

6+
// The program will throw an error because decimalNumber is declared twice in the same function scope. And also decimalNumber is used in console.log(decimalNumber) outside the function, where it doesn't exist.
7+
68
// Try playing computer with the example to work out what is going on
79

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
10+
// Code with errors:
1111

12-
return percentage;
13-
}
14-
15-
console.log(decimalNumber);
12+
// function convertToPercentage(decimalNumber) {
13+
// const decimalNumber = 0.5
14+
//const percentage = `${decimalNumber * 100}%`;
15+
//
16+
// return percentage;
17+
//}
18+
// console.log((decimalNumber));
1619

1720
// =============> write your explanation here
1821

22+
// The error occurs because the variable "decimalNumber" is being declared twice in the same scope. This causes a conflict and results in a syntax error. Additionally, "decimalNumber" is being used in console.log(decimalNumber) outside the function, where it doesn't exist, which will also cause a ReferenceError.
23+
24+
// To fix this error, i simply removed the second declaration of decimalNumber inside the function and also changed console.log(decimalNumber) to console.log(convertToPercentage(0.5)) to call the function with an argument and log the result.
25+
1926
// Finally, correct the code to fix the problem
2027
// =============> write your new code here
28+
29+
function convertToPercentage(decimalNumber) {
30+
const percentage = `${decimalNumber * 100}%`;
31+
32+
return percentage;
33+
}
34+
console.log(convertToPercentage(0.5)); // output: 50%

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
21
// Predict and explain first BEFORE you run any code...
2+
// The code will throw an error because of an unexpected number "3" in the function parameter. The function parameter must be a variable name, not a number.
3+
// Also, there is no num parameter declared in the function, so even if the function definition worked, it would sill cause a reference error when trying to use "num" in the return statement.
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
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 in function parameter list. This error occurs because "3" is not a valid parameter name. Parameters should be variable names that can be used within the function body.
15+
//ReferenceError: num is not defined. This error occurs because "num" is being used in the return statement, but it has not been declared or defined anywhere in the function.
1316

1417
// =============> explain this error message here
18+
// To fix this error, we need to change the function parameter from "3" to a valid variable name, such as "num". This way, we can pass a number as an argument when calling the function, and it will be used correctly within the function body to calculate the square of that number.
1519

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

1822
// =============> write your new code here
1923

20-
24+
function square(num) {
25+
return num * num;
26+
}
27+
console.log(square(3)); // output: 9

0 commit comments

Comments
 (0)