Skip to content

Commit 21ace6b

Browse files
committed
add code and explanations
1 parent b31a586 commit 21ace6b

3 files changed

Lines changed: 51 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The variable inside the function str has already declared as function parameter.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
8+
//function capitalise(str) {
9+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
10+
//return str;
11+
//}
1112

1213
// =============> write your explanation here
14+
// Repeatative variable declaration prediction matches with the error message. Then I changed the old variable into new inside the
15+
// function. Then affter running the code it won't return anything cause no function call has been made yet.
1316
// =============> write your new code here
17+
// This is my code:
18+
function capitalise(str) {
19+
let newstr = `${str[0].toUpperCase()}${str.slice(1)}`;
20+
return newstr;
21+
}
22+
console.log(capitalise("dipa"));

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I thought the function will work properly but couldn't print the correct value cause the value should be convertToPercentage
6+
// but it is declared only as decimalNumber.
57

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+
//function convertToPercentage(decimalNumber) {
11+
//const decimalNumber = 0.5;
12+
//const percentage = `${decimalNumber * 100}%`;
1113

12-
return percentage;
13-
}
14+
//return percentage;
15+
//}
1416

15-
console.log(decimalNumber);
17+
//console.log(decimalNumber);
1618

1719
// =============> write your explanation here
20+
// When I run the code after my prediction it shows SyntaxError: Identifier 'decimalNumber' has already been declared. Which means
21+
// decimalNumber has been declared already as function parameter and then again declared inside the function as variable. Then when I
22+
// declare const decimalNumber = 0.5; outside the function and run the code it shows the value of variable decimalNumber which is 0.5. Then
23+
// I replace console.log(decimalNumber); to console.log(convertToPercentage); and run the code again and it shows this output
24+
// [Function: convertToPercentage] which means only the funtion name. Then I replace console.log(convertToPercentage); to
25+
// console.log(convertToPercentage(decimalNumber)); again and it finally works properly that means converted the decimal number to
26+
// percentage value which shows 50% .
27+
1828

1929
// Finally, correct the code to fix the problem
2030
// =============> write your new code here
31+
// Corrected code:
32+
33+
function convertToPercentage(decimalNumber) {
34+
const percentage = `${decimalNumber * 100}%`;
35+
return percentage;
36+
}
37+
const decimalNumber = 0.5;
38+
console.log(convertToPercentage(decimalNumber));

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@
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 think we cannot directly insert value which is 3 as a parameter. We have to declare it as a variable.
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+
// The error message shows SyntaxError: Unexpected number
1315

1416
// =============> explain this error message here
17+
// The error shows SyntaxError as Unexpected number which means the function syntax doesnot expect to input
18+
// any values directly in the parameter. That's why the function doesnot work.
1519

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

1822
// =============> write your new code here
23+
// The correct code:
24+
25+
function square(num) {
26+
return num * num;
27+
}
28+
let num = 3;
29+
console.log(square(num));
1930

2031

0 commit comments

Comments
 (0)