/
js_algorithms2.js
376 lines (338 loc) · 9.67 KB
/
js_algorithms2.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Sum all the number between the two elements in a given array
function sumAll(arr) {
let x = 0;
let y = 0;
let result = 0;
if (arr[0] <= arr[1]) {
x = arr[0];
y = arr[1];
} else {
x = arr[1];
y = arr[0];
} //x <= y
for (let i=x; i<=y; i++) {
result += i;
}
return result;
}
sumAll([1, 4]);
// OR //
function sumAll(arr) {
var sum = 0;
for (var i = Math.min(...arr); i <= Math.max(...arr); i++) {
sum += i;
}
return sum;
}
sumAll([1, 4]);
// You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
function destroyer(arr) {
let killerArr = [];
for (let i=1; i<arguments.length; i++) {
killerArr.push(arguments[i]);
}
return arr.filter(item => !killerArr.includes(item));
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
// Change a given string to spinal case (all-lowercase-with-hyphens)
function spinalCase(str) {
let myArr = str.match(/[A-Z][a-z]+|[a-z]+/g);
let myStr = "";
for (let i = 0; i < myArr.length; i++) {
myStr += myArr[i];
if (i != myArr.length-1) {
myStr += "-";
}
}
return myStr.toLowerCase();
}
spinalCase('AllThe-small Things');
//Pig Latin - modifying stings - my solution:
function translatePigLatin(str) {
let vowelArr = ['a','e','i','o','u'];
let constStr = "";
let index = 0;
while ((vowelArr.indexOf(str.charAt(index)) < 0) && (index<str.length)) {
constStr += str.charAt(index);
index++;
}
return (constStr === "") ? str += "way" : str = str.slice(constStr.length) + constStr + "ay";
}
translatePigLatin("rythym");
//Or - a different solution using regex
function translatePigLatin(str) {
let consonantRegex = /^[^aeiou]+/;
let myConsonants = str.match(consonantRegex);
return myConsonants !== null
? str
.replace(consonantRegex, "")
.concat(myConsonants)
.concat("ay")
: str.concat("way");
}
translatePigLatin("consonant");
// Matching a object (source) with the obects in an array (collection)
function whatIsInAName(collection, source) {
var arr = [];
let coLen = Object.keys(collection).length;
let searchLen = Object.entries(source).length;
for (let i=0; i<coLen; i++) {
let tempObj = collection[i];
let allMatch = true;
for (let j=0; j<searchLen; j++) {
let x = Object.entries(source)[j][0];
if (source[x] != tempObj[x]) {
allMatch = false;
}
}
if (allMatch) {
arr.push(tempObj);
}
}
return arr;
}
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3});
//Find and replace a string
function myReplace(str, before, after) {
let capsArr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
let tempStr = "";
if (capsArr.includes(before.charAt(0))) {
tempStr = after.charAt(0).toUpperCase() + after.slice(1);
} else {
tempStr = after;
}
let outStr = str.replace(before, tempStr);
return outStr;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
// Missing Letter
function fearNotLetter(str) {
let missingChar;
for (let i=1; i<str.length; i++) {
if ((str.charCodeAt(i-1) + 1) != str.charCodeAt(i)) {
missingChar = String.fromCharCode(str.charCodeAt(i-1)+1);
}
}
return missingChar;
}
fearNotLetter("abce"); //Returns 'd'
fearNotLetter("abcde"); //Returns undefined
// Merge two or more arrays into a single array showing each number only once, and in the order given.
function uniteUnique(arr) {
let inArr = [...arguments].flat();
let outArr = [];
for (let i=0; i<inArr.length; i++) {
if (outArr.indexOf(inArr[i]) === -1) {
outArr.push(inArr[i]);
}
}
return outArr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
// Convert basic symbols to their HTML entities
function convertHTML(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
convertHTML("Dolce & Gabbana"); //Returns: Dolce & Gabbana
//Sum odd fibonacci numbers
function sumFibs(num) {
let index = 1;
let fibNext = 0;
let fibArr = [1,1];
while (num > fibNext) {
fibNext = fibArr[index] + fibArr[index-1];
if (num >= fibNext) {
fibArr.push(fibNext);
}
index++;
}
let result = 0;
for (let i=0; i<fibArr.length; i++) {
if (fibArr[i] % 2 == 1) {
result += fibArr[i];
}
}
return result;
}
sumFibs(100); //Returns: 188 (1+1+3+5+13+21+55+89)
//Sum All Primes
function sumPrimes(num) {
//Find all primes upto and including num
let primeArr = [];
for (let i=2; i<=num; i++) {
let isPrime = true;
for (let j=2; j<i; j++) {
if (i % j === 0) {
isPrime = false;
}
}
if (isPrime === true) {
primeArr.push(i);
}
}
console.log(primeArr);
let result = 0;
for (let i=0; i<primeArr.length; i++) {
result += primeArr[i];
}
console.log(result);
return result;
}
sumPrimes(3); //Result 5 (2+3)
sumPrimes(10); //Result 17 (2+3+5+7)
//Finding the Smallest Common Multiple
function smallestCommons(arr) {
let minVal = arr[0];
let maxVal = arr[0];
if (arr[0] > arr[1]) {
minVal = arr[1];
} else {
maxVal = arr[1];
}
let counter = maxVal;
let result = 0;
let isCommonMultiple = false;
while (isCommonMultiple === false) {
let isSCM = true;
for (let i=minVal; i<=maxVal; i++) {
if (counter % i != 0) {
isSCM = false;
}
}
if (isSCM === true) {
isCommonMultiple = true;
result = counter;
}
counter++;
}
return result;
}
smallestCommons([1,5]);
// A cleaner faster way
const smallestCommons = arr => {
let max = Math.max(...arr);
let min = Math.min(...arr);
// Initially the solution is assigned to the highest value of the array
let solution = max;
for (let i = max - 1; i >= min; i--) {
// Each time the solution checks (i.e. sol%i===0) it won't be necessary
// to increment 'max' to our solution and restart the loop
if (solution % i) {
solution += max;
i = max;
}
}
return solution;
};
// test here
smallestCommons([1, 5]);
//Flatten Array
function steamrollArray(arr) {
return arr.flat(Infinity);
}
steamrollArray([1, [2], [3, [[4]]]]); //Returns: [1,2,3,4]
//Convert a string of binary numbers into a secret text
function binaryAgent(str) {
let inArr = str.split(" ");
let outArr = [];
for (let i=0; i<inArr.length; i++) {
let tempCC = parseInt(Number(inArr[i]), 2);
outArr.push(String.fromCharCode(tempCC));
}
return outArr.join('');
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
//Returns: "Aren't bonfires fun!?"
// Checking for truthy values
function truthCheck(collection, pre) {
// Is everyone being true?
let isTrue = true;
for (let i=0; i<collection.length; i++) {
console.log(collection[i][pre]);
if (collection[i][pre]) {
//do nothing
} else {
isTrue = false;
console.log(collection[i][pre] + " is a falsy value");
}
}
return isTrue;
}
truthCheck([{"single": "double"}, {"single": NaN}], "single"); //Returns: False
// Arguments Optional
function addTogether() {
if (arguments.length === 2) {
if (typeof(arguments[0]) === "number") {
if (typeof(arguments[1]) === "number") {
return arguments[0] + arguments[1];
}
}
} else if (arguments.length === 1) {
if (typeof(arguments[0]) === "number") {
const initArg = arguments[0];
return function addMe(n) {
if (typeof(n) === "number") {
return n + initArg;
} else {
return undefined;
}
}
}
}
return undefined;
}
addTogether(2)(3); //Return 5
// Create a person -- Using Getters and Setters:
var Person = function(firstAndLast) {
let fullName = arguments[0];
let firstName = arguments[0].split(" ")[0];
let lastName = arguments[0].split(" ")[1];
this.getFullName = function() {
return firstName.concat(" ").concat(lastName);
},
this.getFirstName = () => { return firstName; },
this.getLastName = () => { return lastName; },
this.setFirstName = function(first) {
firstName = first;
},
this.setLastName = (last) => {
lastName = last;
},
this.setFullName = (firstAndLast) => {
firstName = firstAndLast.split(" ")[0];
lastName = firstAndLast.split(" ")[1];
fullName = firstName.concat(" ").concat(lastName);
}
};
var bob = new Person('Bob Ross');
console.log(bob.getFirstName()); //Bob
console.log(bob.getLastName()); //Ross
bob.setFirstName('Ricky');
console.log(bob.getFullName()); //Ricky Ross
bob.setFullName('John Doe')
console.log(bob.getFullName()); //John Doe
console.log(bob.getFirstName()); //John
//Maths | Calculating the Orbital Periods of Objects
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
let outArr = [];
for (let i=0; i<arr.length; i++) {
let combinedRadius = earthRadius + arr[i]["avgAlt"];
let orbPeriod = Math.PI * 2 * (Math.sqrt(Math.pow(combinedRadius,3) / GM));
let obj = {name, orbitalPeriod};
obj.name = arr[i].name;
obj.orbitalPeriod = Math.round(orbPeriod);
outArr.push(obj);
}
return outArr;
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
console.log(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])); //[{"name":"sputnik","orbitalPeriod":86400}]