-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment 5.js
106 lines (86 loc) · 3 KB
/
Assignment 5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const validSingleUnits = ['second', 'minute', 'hour', 'day'];
const validPluralUnits = ['seconds', 'minutes', 'hours', 'days'];
const validUnits = [...validPluralUnits, ...validSingleUnits];
const timeAdder = (value1, label1, value2, label2) => {
value1 = validInteger(value1);
label1 = validString(label1) && validUnits.indexOf(label1) > -1 ? label1 : false;
value2 = validInteger(value2);
label2 = validString(label2) && validUnits.indexOf(label2) > -1 ? label2 : false;
let value3 = false;
let output = false;
// Validate Parameters
if (value1 && label1 && value2 && label2) {
// Convert to seconds
value1 = toSeconds(value1, label1);
value2 = toSeconds(value2, label2);
if (value1 && value2) {
value3 = value1 + value2;
output = timeUnits(value3);
}
}
return output;
};
const toSeconds = (value, label) => {
value = validInteger(value);
label = validString(label) && validUnits.indexOf(label) > -1 ? label : false;
output = false;
// Validate Parameters
if (value && label) {
if (validSingleUnits.indexOf(label) > -1) {
value = value === 1 ? value : false;
} else {
value = value !== 1 ? value : false;
}
if (value) {
// Convert to seconds
switch (label) {
case 'minute':
case 'minutes':
output = value * 60;
break;
case 'hour':
case 'hours':
output = value * 3600;
break;
case 'day':
case 'days':
output = value * 86400;
break;
default:
output = value;
break;
}
}
}
return output;
};
const timeUnits = value => {
value = validInteger(value);
const units = [
{ name: 'minute', time: 60 },
{ name: 'hour', time: 60 },
{ name: 'day', time: 24 }
];
let label = 'seconds';
let output = false;
if (value) {
units.forEach(unit => {
if (value % unit.time === 0) {
value = value / unit.time;
label = `${unit.name}${value !== 1 ? 's' : ''}`;
}
});
output = [value, label];
}
return output;
};
const validInteger = integer => (typeof integer === 'number' && integer >= 0 && integer && Number.isInteger(integer) ? integer : false);
const validString = string => (typeof string === 'string' && string.trim().length > 0 ? string.trim() : false);
console.log('True Test 1:', timeAdder(1, 'minute', 3, 'minutes'));
console.log('True Test 2:', timeAdder(5, 'days', 25, 'hours'));
console.log('True Test 3:', timeAdder(1, 'minute', 240, 'seconds'));
console.log('True Test 4:', timeAdder(5, 'days', 5, 'days'));
console.log('True Test 5:', timeAdder(12, 'hours', 12, 'hours'));
console.log('False Test 1:', timeAdder(5, 'hour', 5, 'minutes'));
console.log('False Test 2:', timeAdder(false, false, 5, 'minutes'));
console.log('False Test 3:', timeAdder({}, 'days', 5, 'minutes'));