You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
functionisObjEmpty(obj) {
12
+
returnObject.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.
# 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
+
functionconvertTo24HrsFormat(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