-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntermediateJavascript.js
More file actions
427 lines (361 loc) · 8.52 KB
/
IntermediateJavascript.js
File metadata and controls
427 lines (361 loc) · 8.52 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//Buble sort
let bubbleSort = (inputArr) => {
let len = inputArr.length;
let swapped;
do {
swapped = false;
for (let i = 0; i < len; i++) {
if (inputArr[i] > inputArr[i + 1]) {
let tmp = inputArr[i];
inputArr[i] = inputArr[i + 1];
inputArr[i + 1] = tmp;
swapped = true;
}
}
} while (swapped);
return inputArr;
};
//Sum all
function sumAll(arr) {
var max = Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
var result = 0;
for(var i = min; i <= max; i++) {
result = result + i;
}
return result;
}
sumAll([1, 4]);
//Diff Two Arrays
function diffArray(arr1, arr2) {
var newArr = [];
var element = 0;
for(var index = 0; index < arr1.length; index++)
{
element = arr1[index];
if(arr2.indexOf(element) == -1)
{
newArr.push(element);
}
}
for(var index2 = 0; index2 < arr2.length; index2++)
{
element = arr2[index2];
if(arr1.indexOf(element) == -1)
{
newArr.push(element);
}
}
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
//Roman Numeral Converter
function convertToRoman(num) {
var decimalArray = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var romanArray = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var romanConverted = [];
for(var index = 0; index < decimalArray.length; index++)
{
while(decimalArray[index] <= num)
{
num = num - decimalArray[index];
romanConverted.push(romanArray[index]);
}
}
return romanConverted.join('');
}
convertToRoman(36);
//Wherefore art thou
function whatIsInAName(collection, source) {
var arr = [];
for(var index = 0; index < collection.length; index++)
{
var obj = collection[index];
var found = true;
for(var prop in source)
{
if(source[prop] != obj[prop])
{
found = false;
}
}
if(found)
{
arr.push(collection[index]);
}
}
return arr;
}
//Search and Replace
function myReplace(str, before, after) {
if(before[0].toUpperCase() == before[0])
{
var arr = after.split('');
arr[0] = arr[0].toUpperCase();
after = arr.join('');
}
var newString = str.replace(before, after);
return newString;
}
myReplace("His name is Tom", "Tom", "john");
//Pig Latin
function translatePigLatin(str) {
var vowel = ['a', 'e', 'i', 'o', 'u'];
if(vowel.indexOf(str[0]) != -1)
{
return str + 'way';
}
else if(vowel.indexOf(str[1]) != -1)
{
return str.substr(1) + str[0] + 'ay';
}
else
{
var cluster = [];
var index = 0;
for(index = 0; index < str.length; index++)
{
aux = index;
if(vowel.indexOf(str[index]) == -1)
{
cluster[index] = str[index];
aux = index;
}
else
{
break;
}
}
return str.substr(index) + cluster.join('') + 'ay';
}
}
translatePigLatin("glove");
//DNA Pairing
function pairElement(str) {
var array = [];
for(var index = 0; index < str.length; index++)
{
if(str[index] == 'A')
{
array[index] = ['A','T'];
}
else if(str[index] == 'T')
{
array[index] = ['T','A'];
}
else if(str[index] == 'C')
{
array[index] = ['C','G'];
}
else
{
array[index] = ['G','C'];
}
}
return array;
}
pairElement("GCG");
//Missing letters
function fearNotLetter(str) {
var alphabet = ['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'];
var indexFirst = alphabet.indexOf(str[0]);
for(var index = 0; index < str.length; index++)
{
if(str[index] != alphabet[indexFirst + index])
{
return alphabet[indexFirst + index];
}
}
return undefined;
}
fearNotLetter("abce");
//Boo who
function booWho(bool) {
if(bool === true || bool === false)
return true;
return false;
}
booWho(null);
//Sorted Union
function uniteUnique(arr) {
var result = [];
for(var index = 0; index < arguments.length; index++)
{
for(var index2 = 0; index2 < arguments[index].length; index2++)
{
if(result.indexOf(arguments[index][index2]) == -1)
{
result.push(arguments[index][index2]);
}
}
}
return result;
}
//Convert HTML Entities
function convertHTML(str) {
str = str.split('&').join('&');
str = str.split('<').join('<');
str = str.split('>').join('>');
str = str.split('\"').join('"');
str = str.split("\'").join(''');
return str;
}
convertHTML('Stuff in "quotation marks"');
//Spinal Tap Case
function spinalCase(str) {
str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
str = str.replace(/\s+|_+/g, '-').toLowerCase();
return str;
}
spinalCase("thisIsSpinalTap");
//Sum All Odd Fibonacci Numbers
function sumFibs(num) {
var sum = 0;
var returnSum = 0;
var index = 1;
var previous = 0;
while(index <= num)
{
if(index % 2 !== 0)
{
returnSum = returnSum + index;
}
sum = sum + index;
index = index + previous;
previous = index - previous;
}
return returnSum;
}
sumFibs(4);
//Sum All Primes
function sumPrimes(num) {
var array = [];
var primeSum = 0;
for(var index = 2; index <= num; index++)
{
var isPrime = true;
for(var index2 = 0; index2 < array.length; index2++)
{
if(index % array[index2] === 0)
{
isPrime = false;
break;
}
}
if(isPrime)
{
primeSum += index;
}
array.push(index);
}
return primeSum;
}
sumPrimes(10);
//Smallest Common Multiple
function smallestCommons(arr) {
var quot = 0;
var loop = 1;
var n;
arr.sort(function(a, b) {
return b - a;
});
var newArr = [];
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
while (n !== newArr.length) {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
loop++;
}
return quot;
}
//Finders Keepers
function findElement(arr, func) {
var newArray = arr.filter(func);
return newArray[0];
}
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });
//Drop it
function dropElements(arr, func) {
var newArray = arr.filter(func);
var returnArray = [];
var index = 0;
while(true)
{
if(arr[index] != newArray[0])
{
arr.shift();
}
else
{
break;
}
}
return arr;
}
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})
//Steamroller
function steamrollArray(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? steamrollArray(toFlatten) : toFlatten);
}, []);
}
// test here
steamrollArray([1, [2], [3, [[4]]]]);
//Binary Agents
function binaryAgent(str) {
var binaryArray = str.split(' ');
var result = [];
for(var index = 0; index < binaryArray.length; index++)
{
var value = String.fromCharCode(parseInt(binaryArray[index], 2));
result.push(value);
}
return result.join('');
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
//Everything Be True
function truthCheck(collection, pre) {
for(var index = 0; index < collection.length; index++)
{
var element = collection[index];
if(!element[pre])
{
return false;
}
}
return true;
}
//Algorithm Arguments Optional
function addTogether() {
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if(args.length == 2){
if(typeof args[0] !== 'number' || typeof args[1] !=='number' ){
return undefined;
}
return args[0]+args[1];
}
if(args.length == 1){
a= args[0];
if(typeof a!=='number'){
return undefined;
}
else{
return function(b){
if(typeof b !=='number'){
return undefined;
}
else
return a+b;
};
}
}
}