-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascripttest.html
185 lines (150 loc) · 5.94 KB
/
javascripttest.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<div class="image">
<!--<img src="https://goo.gl/2oZU2S" alt="First">-->
First Image
<button class="remove">X</button>
</div>
<div class="image">
Second Image
<button class="remove1" onclick="registerClickHandler(this)">X</button>
</div>
<button id='btn' type="submit" onclick="myFunc('btn')">
REMOVE THAT!!!
</button>
<div class="image">
addEventListener
<button class="remove">X</button>
</div>
<button type="submit" onclick="solutions()">
Group Array!!!
</button>
<script>
function formatDate(userDate) {
// format from M/D/YYYY to YYYYMMDD
// console.log('userDate', userDate);
// var d = new Date(userDate);
// var str = d.getFullYear() + '' + (d.getMonth() + 1) + '' +d.getDate();
// return str;
}
// console.log('changed format');
// console.log(formatDate("12/31/2014"));
function registerClickHandler (_this) {
// Implement the click handler here for button of class 'remove'
//console.log('event', _this);
//console.log('event', _this.parentNode);
_this.parentNode.remove();
//var el = document.getElementById( _this.id ); //assuming '_this' is the 'this' reference not the 'id'
//el.parentNode.parentNode.removeChild( el.parentNode );
//var divId = document.getElementById(id);
//divId.remove();
}
function myFunc(id) {
var divId = document.getElementById(id);
divId.remove();
};
var classname = document.getElementsByClassName("remove");
var myFunction = function() {
console.log('parentNode ', this.parentNode);
this.parentNode.remove();
//var attribute = this.getAttribute("data-myattribute");
//alert(attribute);
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', myFunction, false);
}
//If you have ES6 support you can replace your last line with:
// Array.from(classname).forEach(function(element) {
// element.addEventListener('click', myFunction);
// });
//Note: Older browsers (like IE6, IE7, IE8) don´t support getElementsByClassName and so they return undefined.
function solutions() {
var myArray = [
{phoneNo: "9804544333", totalSec: 300},
{phoneNo: "9804544333", totalSec: 67},
{phoneNo: "9804544222", totalSec: 60},
{phoneNo: "9804544222", totalSec: 120},
{phoneNo: "9804544111", totalSec: 301},
{phoneNo: "9804544555", totalSec: 300},
{phoneNo: "9804544555", totalSec: 67}
];
// var myArray = [
// {phoneNo: "9804544333", totalSec: 300},
// {phoneNo: "9804544333", totalSec: 67},
// {phoneNo: "9804544222", totalSec: 300}
// ];
var groups = groupFunc(myArray);
//console.log('groups ', groups);
var maxSec = maxSecFunc(groups);
//console.log('maxSec: ', maxSec);
var minNumber = minNumberFunc(groups, maxSec);
//console.log('minNumber: ', minNumber);
var totalCallCost = costCalcualtion(groups, maxSec, minNumber);
console.log('totalCallCost: ', totalCallCost);
}
//Max Cost
function maxSecFunc(groups) {
var maxSec = Math.max.apply(Math,groups.map(function(o){return o.totalSec;}));
return maxSec;
}
//Min Number
function minNumberFunc(groups, maxSec) {
var maxGroupArr = [];
groups.forEach(function(element) {
if(element.totalSec === maxSec){
maxGroupArr.push(element);
}
});
if(maxGroupArr.length > 1){
var minNumber = Math.min.apply(Math,maxGroupArr.map(function(o){return o.phoneNo;}));
}
else var minNumber = null;
return minNumber;
}
//Cost Calcualtions
function costCalcualtion(groups, maxSec, minNumber) {
var totalCallCost = 0;
var callCost = 0;
var mnt = 0;
//console.log('groups: ', groups);
groups.forEach(function(element) {
//console.log('element.totalSec: ', element.totalSec);
if(parseInt(element.phoneNo) === minNumber && element.totalSec === maxSec && maxSec > 300){
console.log('1');
totalCallCost += 0;
//console.log('totalCallCost: ', totalCallCost);
}
else if(minNumber === null && element.totalSec === maxSec && maxSec > 300){
console.log('2');
totalCallCost += 0;
//console.log('totalCallCost: ', totalCallCost);
}
else if(element.totalSec <= 300){
console.log('3');
callCost = element.totalSec * 3;
totalCallCost += parseInt(callCost);
//console.log('totalCallCost: ', totalCallCost);
}
else if(element.totalSec > 300){
console.log('4');
mnt = element.totalSec / 60;
mnt = parseInt(mnt) + 1;
callCost = mnt * 150;
totalCallCost += parseInt(callCost);
//console.log('totalCallCost: ', totalCallCost);
}
});
return totalCallCost;
}
//Grouping an Array
function groupFunc(dataObject) {
var result = dataObject.reduce(function(res, obj) {
if (!(obj.phoneNo in res))
res.__array.push(res[obj.phoneNo] = obj);
else {
//res[obj.phoneNo].hits += obj.hits;
res[obj.phoneNo].totalSec += obj.totalSec;
}
return res;
}, {__array:[]}).__array.sort(function(a,b) { return b.totalSec - a.totalSec; });
return result;
};
</script>