Skip to content

Commit b77568a

Browse files
committed
Challenge 25 & 26 Added
1 parent 23fd7c2 commit b77568a

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ Write a function called `findLongestWord` that takes string as argument `sentenc
218218

219219
[Solution Explanation](./solutions/ch_24_Find_Longest_Word_In_Sentence/readme.md)
220220

221+
## Challenge 25: Check Empty Object
222+
223+
Write a function to check if an object is empty or not.
224+
225+
Write a function called `isObjEmpty` that takes an object `obj` as arguments will return true if it is empty otherwise false.
226+
227+
[Solution Explanation](./solutions/ch_25_Check_Empty_Object/readme.md)
228+
229+
## Challenge 26: Convert Time from 12Hrs to 24Hrs Format
230+
231+
Write a function which can convert time from 12 hours format to 24 hours format.
232+
233+
Write a function called `convertTo24HrsFormat` which take `time` as parameter in `HH:MMAM` format. for example, `12:10AM`.
234+
235+
[Solution Explanation](./solutions/ch_26_Convert_Time_To_24_Format/readme.md)
236+
221237
<!-- Add new challenges before this comment -->
222238

223239
## Contributors
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Function to check whether an object is empty or not
2+
function isObjEmpty(obj) {
3+
return Object.keys(obj).length === 0;
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Challenge 25: Check Empty Object
2+
3+
Write a function to check if an object is empty or not.
4+
5+
Write a function called `isObjEmpty` that takes an object `obj` as arguments will return true if it is empty otherwise false.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to check whether an object is empty or not
11+
function isObjEmpty(obj) {
12+
return Object.keys(obj).length === 0;
13+
}
14+
```
15+
16+
## Answer Explanation
17+
18+
Here is a function called `isObjEmpty` that takes an object as arguments and checking the length of `obj` keys using object `keys()` method and it will return true if length is zero otherwise false.
19+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Function to convert 12Hrs time to 24Hrs time.
2+
function convertTo24HrsFormat(time) {
3+
4+
let timeArray = time.split(':');
5+
let hour = Number(timeArray[0]); // Convert hour into number from string
6+
7+
if (time.endsWith('PM')) {
8+
// if hour is not 12 then add 12 hour.
9+
hour = hour !== 12 ? hour + 12 : hour;
10+
} else {
11+
// if time is AM and if hour is 12 then subtract 12 hour.
12+
hour = hour === 12 ? hour - 12 : hour;
13+
}
14+
time = `${hour.toString().padStart(2, 0)}:${timeArray[1].slice(0, -2)}`;
15+
16+
return time;
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Challenge 26: Convert Time from 12Hrs to 24Hrs Format
2+
3+
Write a function which can convert time from 12 hours format to 24 hours format.
4+
5+
Write a function called `convertTo24HrsFormat` which take `time` as parameter in `HH:MMAM` format. for example, `12:10AM`.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to convert 12Hrs time to 24Hrs time.
11+
function convertTo24HrsFormat(time) {
12+
13+
let timeArray = time.split(':');
14+
let hour = Number(timeArray[0]); // Convert hour into number from string
15+
16+
if (time.endsWith('PM')) {
17+
// if hour is not 12 then add 12 hour.
18+
hour = hour !== 12 ? hour + 12 : hour;
19+
} else {
20+
// if time is AM and if hour is 12 then subtract 12 hour.
21+
hour = hour === 12 ? hour - 12 : hour;
22+
}
23+
time = `${hour.toString().padStart(2, 0)}:${timeArray[1].slice(0, -2)}`;
24+
25+
return time;
26+
}
27+
```
28+
29+
## Answer Explanation
30+
31+
Function `convertTo24HrsFormat` takes `time` as parameter and split it into two parts where first part will be `HH` and second part will be `MMAM` or `MMPM`. After that convert hours into Number data type and check condition using string `endsWith()` method. if condition and manipulate time hours and after that remove AM or PM and return time in 24Hrs format.

0 commit comments

Comments
 (0)