-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreecodecamp Algorithm Advanced.js
More file actions
223 lines (201 loc) · 5.01 KB
/
Copy pathFreecodecamp Algorithm Advanced.js
File metadata and controls
223 lines (201 loc) · 5.01 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//Valid US Numbers
function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
return regex.test(str);
}
//Record Collection
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") {
if(collection[id][prop]) {
collection[id][prop].push(value);
}
else {
collection[id][prop]=[value];
}
}
else if (value !== "") {
collection[id][prop] = value;
}
else {
delete collection[id][prop];
}
return collection;
}
//Symmetric Difference
function sym() {
var args = [];
for(var i = 0; i < arguments.length; i++){
args.push(arguments[i]);
}
function find(arr1, arr2){
var answer = [];
arr1.forEach(function(item){
if(arr2.indexOf(item) < 0 && answer.indexOf(item) < 0) {
answer.push(item);
}
});
arr2.forEach(function(item){
if(arr1.indexOf(item) < 0 && answer.indexOf(item) < 0) {
answer.push(item);
}
});
return answer;
}
return args.reduce(find);
}
//Map the Debris
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
var a = 2 * Math.PI;
var newArr = [];
var getOrbPeriod = function(obj) {
var c = Math.pow(earthRadius + obj.avgAlt, 3);
var b = Math.sqrt(c / GM);
var orbPeriod = Math.round(a * b);
delete obj.avgAlt;
obj.orbitalPeriod = orbPeriod;
return obj;
};
for (var elem in arr) {
newArr.push(getOrbPeriod(arr[elem]));
}
return newArr;
}
//Make a Person
var Person = function(firstAndLast) {
var fullName = firstAndLast;
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(name) {
fullName = name + " " + fullName.split(" ")[1];
};
this.setLastName = function(name) {
fullName = fullName.split(" ")[0] + " " + name;
};
this.setFullName = function(name) {
fullName = name;
};
};
//Inventory updates
function updateInventory(arr1, arr2) {
var index;
var getProductIndex = function (name) {
for (var i = 0; i < this.length; i++) {
if (this[i][1] === name) {
return i;
}
}
return undefined;
};
for (var i = 0; i < arr2.length; i++) {
index = getProductIndex.call(arr1, arr2[i][1]);
if (index === undefined) {
arr1.push(arr2[i]);
} else {
arr1[index][0] += arr2[i][0];
}
}
arr1.sort(function (a, b) {
if (a[1] > b[1]) {
return 1;
}
if (a[1] < b[1]) {
return -1;
}
return 0;
});
return arr1;
}
//Exact Change
var denom = [
{ name: 'ONE HUNDRED', val: 100.00},
{ name: 'TWENTY', val: 20.00},
{ name: 'TEN', val: 10.00},
{ name: 'FIVE', val: 5.00},
{ name: 'ONE', val: 1.00},
{ name: 'QUARTER', val: 0.25},
{ name: 'DIME', val: 0.10},
{ name: 'NICKEL', val: 0.05},
{ name: 'PENNY', val: 0.01}
];
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var register = cid.reduce(function(acc, curr) {
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, {total: 0});
if (register.total === change) {
return 'Closed';
}
if (register.total < change) {
return 'Insufficient Funds';
}
var change_arr = denom.reduce(function(acc, curr) {
var value = 0;
while (register[curr.name] > 0 && change >= curr.val) {
change -= curr.val;
register[curr.name] -= curr.val;
value += curr.val;
change = Math.round(change * 100) / 100;
}
if (value > 0) {
acc.push([ curr.name, value ]);
}
return acc;
}, []);
if (change_arr.length < 1 || change > 0) {
return "Insufficient Funds";
}
return change_arr;
}
//Pairwise
function pairwise(arr, arg) {
var sum = 0;
var pairArr = arr.slice();
for(i = 0; i < pairArr.length; i++) {
for(j = i + 1; j < pairArr.length; j++) {
if(pairArr[i] + pairArr[j] == arg) {
sum += i + j;
pairArr[i] = pairArr[j] = NaN;
}
}
}
return sum;
}
//No Repeats Please
function permAlone(str) {
var regex = /(.)\1+/g;
var arr = str.split('');
var permutations = [];
var tmp;
if (str.match(regex) !== null && str.match(regex)[0] === str) return 0;
function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
function generate(int) {
if (int === 1) {
permutations.push(arr.join(''));
} else {
for (var i = 0; i != int; ++i) {
generate(int - 1);
swap(int % 2 ? 0 : i, int - 1);
}
}
}
generate(arr.length);
var filtered = permutations.filter(function(string) {
return !string.match(regex);
});
return filtered.length;
}