Skip to content

Commit 80d9aff

Browse files
committed
1-key-errors Done
1 parent 8472bf4 commit 80d9aff

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Predict and explain first...
2+
// the code is supposed to convert a decimal number to percentage by multiplying
3+
// the decimal Number by 100 then add the sign % to show that it is a percentage
24

35
// Why will an error occur when this program runs?
46
// =============> write your prediction here
7+
// an error will occur because the variable decimalNumber is redeclared inside the function.
8+
// it's already declared as a parameter of the function, so redeclaring it with const causes a syntax error.
59

610
// Try playing computer with the example to work out what is going on
711

@@ -15,6 +19,18 @@ function convertToPercentage(decimalNumber) {
1519
console.log(decimalNumber);
1620

1721
// =============> write your explanation here
22+
// the fucntion convertToPercentage has a parameter named decimalNumber,
23+
// but inside the function, there is a line that tries to declare a new constant with the same name decimalNumber.
24+
// there is no need to redeclare the variable or edit it since it is a user input.
25+
// also the variable decimalNumber is called from outside the function which menas from another scope.
26+
// that is wrong because the variable can only be used inside the fucntion. instead of that we need to call the funtion, not hte variable.
1827

1928
// Finally, correct the code to fix the problem
2029
// =============> write your new code here
30+
31+
function convertToPercentage(decimalNumber) {
32+
const percentage = `${decimalNumber * 100}%`;
33+
return percentage;
34+
}
35+
36+
console.log(convertToPercentage(0.3));

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,32 @@
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 will occur because the parameter name is a number (3) which is not allowed.
8+
// it should be a variable or empty.
79

810
function square(3) {
911
return num * num;
1012
}
1113

1214
// =============> write the error message here
13-
15+
// SyntaxError: Unexpected number
16+
// at internalCompileFunction (node:internal/vm:73:18)
17+
// at wrapSafe (node:internal/modules/cjs/loader:1274:20)
18+
// at Module._compile (node:internal/modules/cjs/loader:1320:27)
19+
// at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
20+
// at Module.load (node:internal/modules/cjs/loader:1197:32)
21+
// at Module._load (node:internal/modules/cjs/loader:1013:12)
22+
// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
23+
// at node:internal/main/run_main_module:28:49
24+
25+
// Node.js v18.19.1
1426
// =============> explain this error message here
15-
27+
// ther error message says that a number can not put as an input when declaring the function
1628
// Finally, correct the code to fix the problem
1729

1830
// =============> write your new code here
31+
function square(num) {
32+
return num * num;
33+
}
1934

20-
35+
console.log(square(5));

0 commit comments

Comments
 (0)