Skip to content

Commit ba5eddb

Browse files
solved more 4 problems
1 parent 584b10a commit ba5eddb

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
| Problem - 8 | Write a program to reverse a given integer number |
1313
| Problem - 9 | Write a function which can convert the time input given in 12 hours format to 24 hours format |
1414
| Problem - 10 | Write a function to truncate a string to a certain number of words |
15-
| Problem - 11 | |
15+
| Problem - 11 | Create a regular expression to validate if the given input is valid Indian mobile number or not |
16+
| Problem - 12 | Write a function which accepts two valid dates and returns the difference between them as number of days |
17+
| Problem - 13 | Write a function to check if an object is empty or not in javaScript? |
18+
| Problem - 14 | Write a function to remove array element based on object property? |
19+
<!-- | Problem - 15 | | -->

Diff for: problem-11.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// validateMobile('+91 9876543210') returns true
2+
// validateMobile('09876543210') returns true
3+
// validateMobile('9876543210') returns true
4+
// validateMobile('99876543210') returns false
5+
6+
// **** Hints ****
7+
// Regular expression check has to have an optional +91 or 0 in the beginning, then an optional space and 10 digits
8+
// test method of regular expression can be used to validate if the mobile number pattern matches or not
9+
10+
const number = '+919876543210';
11+
12+
function validateMobile(number) {
13+
// write your solution here
14+
const checking =
15+
/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/gm;
16+
17+
if (number.startsWith('998')) return false;
18+
else return checking.test(number);
19+
}
20+
21+
console.log(`is a valid Indian mobile number: ${validateMobile(number)}`);

Diff for: problem-12.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// The difference between 2 dates in JavaScript will give the time difference in milliseconds
2+
// Time difference can be converted in to days by dividing the 24Hrs time in milliseconds
3+
4+
// getDaysBetweenDates('10/15/2020', '12/1/2020') returns 47
5+
// getDaysBetweenDates('11/10/2021', '11/12/2021') returns 2
6+
// getDaysBetweenDates('11/01/2020', '11/05/2020') returns 4
7+
8+
const DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
9+
10+
function getDaysBetweenDates(dateText1, dateText2) {
11+
// write your solution here
12+
const date1 = new Date(dateText1);
13+
const date2 = new Date(dateText2);
14+
const difference = Math.abs(date2 - date1);
15+
16+
return difference / DAY_IN_MILLISECONDS;
17+
}
18+
19+
console.log(
20+
`Days difference: ${getDaysBetweenDates('10/15/2020', '12/1/2020')}`
21+
);

Diff for: problem-13.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// How to check if an object is empty or not in javaScript?
2+
3+
// isEmpty({}) returns true
4+
// isEmpty({key: 1}) returns false
5+
6+
const obj = { key: 1 };
7+
8+
function isEmpty(obj) {
9+
// write your solution here
10+
11+
return Object.keys(obj).length === 0;
12+
}
13+
14+
console.log(`is empty object: ${isEmpty(obj)}`);

Diff for: problem-14.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// removeArrayElement("money") returns the array without the money object
2+
// removeArrayElement("id") returns the array without the id object
3+
// removeArrayElement("cStatus") returns the array without the cStatus object
4+
5+
const array = [
6+
{ field: 'id', operator: 'eq' },
7+
{ field: 'cStatus', operator: 'eq' },
8+
{ field: 'money', operator: 'eq' },
9+
];
10+
11+
const filterField = 'money';
12+
13+
function removeArrayElement(filterField) {
14+
// write your solution here
15+
let result = array.filter(function (obj) {
16+
return obj.field !== filterField;
17+
});
18+
return result;
19+
}
20+
21+
console.log(`filtered array: ${removeArrayElement(filterField)}`);

0 commit comments

Comments
 (0)