Skip to content

Commit 8472bf4

Browse files
committed
exercise 1.2 done
1 parent 6959f9a commit 8472bf4

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
.DS_Store
33
.vscode
44
testing.js --- IGNORE ---
5+
testing.js
56
**/.DS_Store

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// call the function capitalise with a string input
99
// interpret the error message and figure out why an error is occurring
10-
@@ -10,4 +14,14 @@ function capitalise(str) {
10+
function capitalise(str) {
1111
}
1212

1313
// =============> write your explanation here

testing.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
function capitaliseFirstLetter(string){
2-
string = `${string[0].toUpperCase()}`;
3-
return string;
1+
// 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
4+
5+
// Why will an error occur when this program runs?
6+
// =============> 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.
9+
10+
// Try playing computer with the example to work out what is going on
11+
12+
function convertToPercentage(decimalNumber) {
13+
const decimalNumber = 0.5;
14+
const percentage = `${decimalNumber * 100}%`;
15+
16+
return percentage;
17+
}
18+
19+
console.log(decimalNumber);
20+
21+
// =============> 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.
27+
28+
// Finally, correct the code to fix the problem
29+
// =============> write your new code here
30+
31+
function convertToPercentage(decimalNumber) {
32+
const percentage = `${decimalNumber * 100}%`;
33+
return percentage;
434
}
535

6-
console.log(capitaliseFirstLetter("hello"));
36+
console.log(convertToPercentage(0.3));

0 commit comments

Comments
 (0)