- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeadder.js
More file actions
Latest commit
98 lines (85 loc) · 2.33 KB
/
Copy pathtimeadder.js
File metadata and controls
98 lines (85 loc) · 2.33 KB
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
constisInteger=(value)=>!isNaN(value)&&value>=0;
constvalidLabels=["seconds","minutes","hours","days","second","minute","hour","day"];
constisLabelInList=(label)=>validLabels.indexOf(label)!==-1;
constisValidLabel=(value,label)=>{
switch(label){
case"second":
case"minute":
case"hour":
case"day":
return(value===1) ? true : false;
case"seconds":
case"minutes":
case"hours":
case"days":
return(value===1) ? false : true;
default:
console.log("Label not valid.");
returnfalse;
}
}
constminuteSeconds=60;
consthourSeconds=minuteSeconds*60;
constdaySeconds=hourSeconds*24;
constconvertToSeconds=(value,label)=>{
switch(label){
case"second":
case"seconds":
returnvalue;
case"minute":
case"minutes":
returnvalue*minuteSeconds;
case"hour":
case"hours":
returnvalue*hourSeconds;
case"day":
case"days":
returnvalue*daySeconds;
default:
console.log("Can't convert to seconds.");
returnfalse;
}
}
constconvertToLarger=(seconds)=>{
if(seconds%daySeconds===0){
constdays=seconds/daySeconds;
return(days===1) ? [days,'day'] : [days,'days'];
}elseif(seconds%hourSeconds===0){
consthours=seconds/hourSeconds;
return(hours===1) ? [hours,'hour'] : [hours,'hours'];
}elseif(seconds%minuteSeconds===0){
constminutes=seconds/minuteSeconds;
return(minutes===1) ? [minutes,'minute'] : [minutes,'minutes'];
}else{
return(seconds===1) ? [seconds,'second'] : [seconds,'seconds'];
}
}
consttimeAdder=(value1,label1,value2,label2)=>{
// check values
if(!isInteger(value1)){
returnfalse;
}
if(!isInteger(value2)){
returnfalse;
}
// check labels
if(!isLabelInList(label1)){
returnfalse;
}
if(!isLabelInList(label2)){
returnfalse;
}
// check values and labels
if(!isValidLabel(value1,label1)){
returnfalse;
}
if(!isValidLabel(value2,label2)){
returnfalse;
}
// convert values to seconds
constseconds1=convertToSeconds(value1,label1);
constseconds2=convertToSeconds(value2,label2);
// add seconds
constresult=seconds1+seconds2;
returnconvertToLarger(result);
}