-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
171 lines (143 loc) · 4.02 KB
/
app.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
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
var structure = {
"_id": "E241AD11-1557-4FD3-B21F-97D0FA4230C7",
"_rev": "13-2c82a95b6558a4c6144b58f59720edc7",
"fp_type": "ibeacon",
"fp_owner": "global",
"fp_ext_id": "bstg_autogen_1445345099899_62005859",
"uuid": "B9407F30-F5F8-466E-AFF9-25556B57FE6D",
"major": 37100,
"minor": 51516,
"label": "Icy Bond",
"actions": [
{
"type": "home-block",
"params": {
"appearance": "text",
"action": "open-location",
"link": {
"fp_type": "sponsor",
"fp_ext_id": "spotme"
}
}
},
{
"type": "presence",
"params": {
"interval": 15
}
}
],
"fp_last_changes": {
"timestamp": 1445437699,
"source": "dataloader_api",
"host": "localhost.backstage.4pax.com"
},
"location": {
"lat": -4.232423,
"lng": 12.523098
}
};
document.getElementById("json").value = JSON.stringify(structure);
//function for adding a new action type notification with parameters title and message
function NewNotification(title, message){
var newAction = {"type":"notification",
"params":
{
"title":title,
"message":message
}
};
structure.actions.push(newAction);
console.log(structure);
document.getElementById("json").value = structure;
}
//function for setting action of the first property to null and remove link property
function SetNull(){
structure.actions[0].params.action=null;
delete structure.actions[0].params.link;
console.log(structure);
document.getElementById("json").value = structure;
}
//function with UnderScore extract actions
function ExtractUnderscore(){
var actionsExtracted = _.pick(structure,'actions');
var result = _.map(actionsExtracted.actions, function(value, key){
return { key : value.type, value : value.params };
});
console.log(result);
}
//function for the sum with underscoe
function SumUnderscore(){
var input=["0", 2, 4, 6, null, [], 8, 10];
var sum = _.reduce(input, function(memo, num){
if(Number.isInteger(num)){
return memo + num;
}
else{
return memo;
}
}, 0);
var avg= sum/input.length;
console.log("Sum is: "+sum+" and Average is: "+avg);
}
//In the above task, what would happen if there were two actions of a same type?
//Then it will be an object with 2 keys repeated, which is incorrect because key should be unique
//function api
function GetIp(){
$.ajax({
url: "https://api.ipify.org?format=json",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data) {
console.log(data);
},
error: function(){
alert("Cannot get data");
}
});
}
function testEach(){
var people = ['Tom','Dick','Larry'];
var Tom = {'name':'Tom','age':'29'};
_.each(people, function(value,index){
console.log(value, index);
});
_.each(Tom, function(value,key){
if(key == 'name'){
console.log(value);
}
});
}
function testMap (){
var numbers = [1,2,3];
var numberMultiple = _.map(numbers, function(value,index,numbers){
numbers[index] = numbers[index] * 2;
return value * 3 ;
});
console.log(numbers);
console.log(numberMultiple);
}
function testReduce (){
var numbers = [1,2,3];
var sum = _.reduce(numbers, function(total,value,index,numbers){
return total + sum;
});
console.log(numbers);
}
function testFind (){
var numbers = [1,2,3,5,6];
var found = _.find(numbers, function(num){
return num % 3 == 0;
});
console.log(found);
}
function testFilter (){
var numbers = [1,2,3,5,6];
var found = _.filter(numbers, function(num){
return num % 3 == 0;
});
console.log(found);
}
//Each in UnderScore