Skip to content

Commit c9700bf

Browse files
committed
Key error done
The codes are fixed, the error has been corrected and explained
1 parent 113d089 commit c9700bf

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> This code will give an error message
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,10 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
// =============> it gave an error because the variable str is being declared twice one with the function and another one with anew variable
13+
// =============> this can be fixed by changing the variable name inside the function
14+
function capitalise(str) {
15+
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
16+
return capitalised;
17+
}
18+
console.log(capitalise("sophia")); // Output: "Sophia"

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> I can see that the variable decimalNumber is being declared twice once as a function parameter and again as a constant
55

66
// Try playing computer with the example to work out what is going on
77

@@ -14,7 +14,18 @@ function convertToPercentage(decimalNumber) {
1414

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
17+
// =============> so the function is to convert a decimal number into a percentage
18+
// so if the number is 0.5 it is multiplied by 100
19+
// % sign is added at the end to make it 50%
1820

1921
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
22+
// =============> the problem can be fixed by removing the redeclaration of the variable decimalNumber inside the function
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
return percentage;
26+
}
27+
28+
// Call the function and store the result in a variable
29+
const result = convertToPercentage(0.5);
30+
console.log(result);
31+

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33

44
// this function should square any number but instead we're going to get an error
55

6-
// =============> write your prediction of the error here
6+
// =============> First there is a number inside the function which is not allowed in JavaScript it should be a variable name
7+
//the second mistake is num is not declared
78

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

12-
// =============> write the error message here
13+
// =============> Uncaught SyntaxError: Unexpected number
14+
// =============> we put a number (3)where JavaScript expected a variable name
15+
//Uncaught SyntaxError: Illegal return statement
1316

14-
// =============> explain this error message here
17+
//we cant use return inside the function without declaring the function properly
1518

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

18-
// =============> write your new code here
21+
22+
// =============>
23+
function square(num) {
24+
return num * num;
25+
}
26+
console.log(square(3)); // the answer should be 9
27+
console.log(square(5)); // the answer should be 25
28+
console.log(square(10)); // the answer should be 100
29+
1930

2031

0 commit comments

Comments
 (0)