Skip to content

Commit 5b3cd1e

Browse files
committed
Exercise 5 done
1 parent be15364 commit 5b3cd1e

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

testing.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
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;
1+
function formatAs12HourClock(time) {
2+
if (Number(time.slice(0, 2)) > 12)
3+
{
4+
return `${Number(time.slice(0, 2) - 12)}:00 pm`;
5+
}
6+
return `${time} am`;
347
}
358

36-
console.log(convertToPercentage(0.3));
9+
let targetOutPut = "2:00 pm";
10+
let currentOutPut= formatAs12HourClock("14:00");
11+
12+
console.assert(
13+
targetOutPut === currentOutPut,
14+
`current output: ${currentOutPut}, target output: ${targetOutPut}`
15+
);
16+
// =========
17+
targetOutPut = "11:00 am";
18+
currentOutPut= formatAs12HourClock("11:00");
19+
20+
console.assert(
21+
currentOutPut === targetOutPut,
22+
`Error,, current is ${currentOutPut} and target is ${targetOutPut}`
23+
);

0 commit comments

Comments
 (0)