-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
134 lines (107 loc) · 4.29 KB
/
script.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Question No. 01 */
let hoursToSec = () => {
var hours = document.getElementById("hours").value;
let seconds = hours * 60 * 60;
if(hours === '') {
document.getElementById("ans1").innerHTML = '';
} else {
document.getElementById("ans1").innerHTML = `${hours} Hours   =   ${seconds} Seconds`;
}
}
/* Question No. 02 */
let calcAge = () => {
var age = document.getElementById("age").value;
let days = age * 365;
if(age === '') {
document.getElementById("ans2").innerHTML = '';
} else if (age < 0 ) {
document.getElementById("ans2").innerHTML = 'Please Enter a Positive Integer...';
} else {
document.getElementById("ans2").innerHTML = `Your Age in Days is:   ${days} Days`;
}
}
/* Question No. 03 */
let invertArray = () => {
var arr = document.getElementById("arr").value;
if(arr === '' || arr === "[]") {
document.getElementById("ans3").innerHTML = "[]";
}
let inverArr = [];
//following RegEx will match any string that contains zero or one occurrence of - and after it will match any digit globally
let num = arr.match(/-?(\d+)/g);
for(let i = 0; i < num.length; i++) {
num[i].slice(",");
//it will change sign and push all the elements one-by-one in the array
inverArr.push(parseInt(num[i]) * -1);
}
//converts inverArr into object format and add space after comma
inverArr = JSON.stringify(inverArr).replace(/,/g, ", ");
document.getElementById("ans3").innerHTML = `${inverArr}`;
}
/* Question No. 04 */
let comp2Str = (str1, str2) => {
var str1 = document.getElementById("str1").value;
var str2 = document.getElementById("str2").value;
if(str1 === '' && str2 === '') {
document.getElementById("ans4").innerHTML = '';
} else if(str1 === '' || str2 === '') {
document.getElementById("ans4").innerHTML = "Please fill both fields...";
} else if(str1.length === str2.length) {
document.getElementById("ans4").innerHTML = "TRUE";
} else {
document.getElementById("ans4").innerHTML = "FALSE";
}
}
/* Question No. 05 */
let parseArray = () => {
var arr2 = document.getElementById("arr2").value;
//following replace will eradicate any string's quotes either "" or ''
let newArr = arr2.replace(/\"|\'/g, "");
//following will split all the elements and convert all the individuals into string
let parsedArr = JSON.stringify(newArr.split(", "));
parsedArr = parsedArr.replace(/,/g, ",   ");
if(arr2 === '' || arr2 === "[]") {
document.getElementById("ans5").innerHTML = '[]';
} else {
document.getElementById("ans5").innerHTML = parsedArr;
}
}
/* Question No. 06 */
let leapYear = () => {
var year = document.getElementById("year").value;
if(year === '') {
document.getElementById("ans6").innerHTML = '';
} else if(year % 4 === 0) {
document.getElementById("ans6").innerHTML = `${year} is a Leap Year.`;
} else {
document.getElementById("ans6").innerHTML = `${year} is not a Leap Year.`;
}
}
/* Question No. 07 */
let endCorona = () => {
var recoverCases = document.getElementById("id1").value;
var newCases = document.getElementById("id2").value;
var activeCases = document.getElementById("id3").value;
if(recoverCases === '' || newCases === '' || activeCases === '') {
document.getElementById("ans7").innerHTML = '';
} else {
recoverCases = parseInt(recoverCases); newCases = parseInt(newCases); activeCases = parseInt(activeCases);
let days = 0;
if(recoverCases > newCases) {
while(true) {
activeCases = (activeCases + newCases) - recoverCases;
days++;
if(activeCases === 0) {
break;
} else if(activeCases + newCases < recoverCases) {
let lastDay = (activeCases + newCases) / recoverCases;
days = Math.round(days + lastDay);
break;
}
}
document.getElementById("ans7").innerHTML = days;
} else {
document.getElementById("ans7").innerHTML = "Recovered Cases will be Greater than New Cases.";
}
}
}