-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodeSnipsToOrganize.js
468 lines (388 loc) · 10.1 KB
/
codeSnipsToOrganize.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* A 'concatAll' function used to merge an array of arrays, only works one
level deep but utilizes push.apply- kinda neat. Originally sourced from r
reactivex demo
*/
let nest = [[1,2,3],[2,3,5,1,23,3,2],[1,2,3,2,1]];
let concatAll = function(arr) {
var results = [];
arr.forEach(function(subArray) {
results.push.apply(results, subArray);
});
return results;
};
console.log(concatAll(nest));
/*These are two examples of how to turn a number that is a string into an
actually spelled out number as in "1" to "one" etc. The first one is bulky
and not so delicious, but the second seems to do a decent job, albeit the
problem of when numbers are touch other strings has yet to be addressed as
do the issues of multiple digit numbers, one suggestion was also to have my
source be an object rather than an array and this makes pretty good sense,
next round
*/
let str = "I have 10 dogs and 6 ponies";
let numToText = (phrase) =>
phrase
.split(' ')
.reduce((newStr, item)=>{
if (item === '1') return newStr.concat('one');
if (item === '2') return newStr.concat('two');
if (item === '3') return newStr.concat('three');
if (item === '4') return newStr.concat('four');
if (item === '5') return newStr.concat('five');
if (item === '6') return newStr.concat('six');
if (item === '7') return newStr.concat('seven');
if (item === '8') return newStr.concat('eight');
if (item === '9') return newStr.concat('nine');
if (item === '10') return newStr.concat('ten');
if (item === '0') return newStr.concat('zero');
return newStr.concat(item);
}, [] );
let numStrings2 =
['zero', 'one', 'two', 'three', 'four','five','six',
'seven', 'eight', 'nine','ten'];
let numToText2 = (phrase)=>
phrase
.split('')
.reduce((newPhrase, item, i) =>{
if(!isNaN(item)){
return newPhrase.concat(numStrings2[parseInt(item)]);
}
return newPhrase.concat(item);
}, []);
console.log(numToText2(str).join(' '));
});
//Counting parentheses step problem
let parens = "((())))((((((";
let countParen = (parenths) =>
parenths
.split('')
.reduce((count, item, i)=>{
if(item === '(') count++;
if(item === ')') count--;
if(count <= 0) count = 0;
return count;
}, 0);
console.log(countParen(parens));//returns 3
let parens = "((())))((((((";
let countParen = (parenths) =>
parenths
.split('')
.reduce((count, item, i)=>{
if(item === '(') count++;
if(item === ')') count--;
if(count <= 0) count = 0;
return count;
}, 0);
console.log(countParen(parens));//returns 3
//mapObject: maps an object and takes in a callback
let mapObject = (object, action, exception) => {
let transObject = {};
for(var key in object){
transObject[key] = action(object[key], key, object);
}
return transObject;
}
let sanitize = (value, key) => {
if (value === exception)return value;
return value.toUpperCase();
}
console.log(mapObject(input, sanitize));
/////mapObject
let input=
{
first_name: "James",
last_name: "Hu",
gender: "Male",
biography: "Oh hey, I'm just a guy"
}
//I: Input- Source Object
//O: Output- Object with altered values
//C: Contraints - do not apply callback to last item
//E: Edge Cases: what if it's not an object
//declare a map object
//with arguments of object and callback function
//if(object[key] === exception)
let mapObject = (object, action) => {
let transObject = {};
for(var key in object){
console.log(key);
if(key === 'biography'){
transObject[key] = object[key];
} else {
transObject[key] = action(object[key], key, object);
}
}
return transObject;
}
let sanitize = (value, key) => {
return value.toUpperCase();
}
mapObject(input, sanitize);
//RECURSION!!!!/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
//sum using recursion
var sumRecursion
= function(array) {
sum += array[count]
count ++
if (count < array.length) {
sumRecursion
(array);
} else {
return sum
}
}
// 3. Sum all numbers in an array containing nested arrays.
//let arraySum = [1,[2,3],[[4]],5];
// sum flatten with Antoine
var arraySum = function(array) {
let count = array.length - 1;
console.log(array, "lenght" + array.length + " count" + count);
//let sum = 0;
if(count < 1){
console.log("term");
return arraySum(array[0]);
}
if (Array.isArray(array[count])){
return arraySum(array.pop()) + arraySum(array)
}
else if (array.length ){
return array[count] + arraySum(array.slice(0, array.length -1))
}
else{
//arraySum(array);
return array
}
//return arraySum(array.slice(0, array.length -1));
};
console.log(arraySum([1,[2,3],[[4]],5,1,[2,[2]],[1,2,[[2,3,4]]]]));
var sum = 0
var count = 0
sumRecursion
([1,2,3]);
console.log(sum);
//A better sum sumthing- sum an array of integers
let pracRay = [1,2,3,4,5];
let sum = (arr, num) =>{
if(num === 1)return num;
return num += sum(arr, num - 1);
}
console.log(sum(pracRay, pracRay.length));
//factorial with recursion
let factorial = (num) =>{
if(num === 0)return 1;
return num * factorial(num - 1);
}
console.log(factorial(4));
// Sum all numbers in an array containing nested arrays.
var arraySum = function(array) {
let count = array.length - 1;
console.log(array, "length " + array.length + " count " + count);
if(count < 1){
console.log("term");
return arraySum(array[0]);
}
if (Array.isArray(array[count])){
return arraySum(array.pop()) + arraySum(array)
}
else if (array.length ){
return array[count] + arraySum(array.slice(0, array.length -1))
}
else{
return array
}
};
console.log(arraySum([1,[2,3],[[4]],5,1,[2,[2]],[1,2,[[2,3,4]]]]));
//REDUCE!!!!!
//Largest number in an Array
var ratings = [2,3,1,9,4,5];
console.log(ratings.reduce((largest, num ) =>{
console.log(largest, num);
if(largest < num) largest = num;
return largest;
},0));
//METHODS:
//difference between push and concat
var arr = ['a', 'b', 'c'];
var x = arr.concat('d');
console.log(arr);//logs ['a', 'b', 'c', 'd']
console.log(x);// logs length of the array
var arr1 = ['a', 'b', 'c'];
var x1 = arr.concat('d');
console.log(arr1);//logs ['a', 'b', 'c']
console.log(x1);// logs['a', 'b', 'c', 'd']
//sorting
var y = [1, 9, 3, 100, 800, 20].sort(function(a,b){
return a - b;
})
$("li.recording").on('click', (e) => {
})
///Ali Practice
let input=
{
first_name: "James",
last_name: "Hu",
gender: "Male",
biography: "Oh hey, I'm just a guy"
}
//I: Input- Source Object
//O: Output- Object with altered values
//C: Contraints - do not apply callback to last item
//E: Edge Cases: what if it's not an object
//declare a map object
//with arguments of object and callback function
//if(object[key] === exception)
let mapObject = (object, action) => {
let transObject = {};
for(var key in object){
console.log(key);
if(key === 'biography'){
transObject[key] = object[key];
} else {
transObject[key] = action(object[key], key, object);
}
}
return transObject;
}
let sanitize = (value, key) => {
return value.toUpperCase();
}
mapObject(input, sanitize);
//SetTimeout_TIMEOUT fun
setTimeout(function(){
asyc1 += 1;
}, 1000);
var async1 = 0;
for (var i = 10; i < 13; i++){
setTimeout(() =>{
}, i);
}
setTimeout (() =>{
console.log(async1);
done();
},200);
///garbage experiment with objects and delete
let obj = {
name: "james",
lastname: "goedert",
age: 31,
}
console.log(Object.keys(obj));
console.log(obj);
obj.age = null;
//console.log(obj);
delete(obj.age);
//console.log(obj);
delete(obj.lastName);
console.log(obj);
obj.lastname = null;
delete obj.lastname;
console.log(obj);
//obj.name = null;
delete obj.name;
console.log(obj);
console.log(Object.keys(obj));
// let keyCount =(obj, key)=>{
// let keys = Object.keys(obj);
// if(obj === []){
// };
// }
//code dungeon
console.clear();
//range
let range = (n1,n2) =>{
console.log
if ( n1 === n2){
return [n1];
}
return [n1].concat(range(n1 + 1, n2));
}
console.log(range(2, 6));
//console.clear();
var count =0;
var divide = function(x, y) {
console.log(`comes in as ${x}`)
//count ++;
if(x < 1) return count;
let result = divide(x - y, y) - y;//doesn't need the - y crazy pants
console.log(count ++);
console.log(`results in ${result}`);
return count;
/////Ali's recursion reverse examples:
// // let str = "hello";
// // var reversStr = function(str){
// // if(str.length < 1)return str;
// // return str(1)
// // }
// //recursion with inner function
// var reversStr1 = function(str){
// var res = '';
// var inner = function(res, left){
// if(!str.length){
// return;
// } else {
// res += left[0];
// return inner(res, left.slice(1));
// }
// }
// inner(res, str);
// return res;
// }
// console.log(reverseStr(str));
// //without result
// //recursion with inner function
// var reversStr1 = function(str){
// var res = '';
// var inner = function(left){
// if(!left.length){
// return;
// } else {
// res += left[left.length - 1];
// return inner( left.slice(-1));
// }
// }
// inner(str);
// return res;
// }
// console.log(reverseStr1('hello'));
//accessing params
// var reverseStr2 = function(str){
// var res = Array.from(arguments)[1] || '';//making an argument
// if(!str.length){
// return res;
// }
// res += str[str.length - 1];
// return reverseStr2(str.slice(0,-1), res);
// }
// console.log(reverseStr2('hello'));
var reverseStr3 = function(str){
if(!str.length){
return '';
}
return str[str.length-1] + reverseStr(str.slice(0,-1));
}
console.log(reverseStr3('hello'));
//factorial recursive with ali
var factorial = function(num, res = 1){
console.log(res);
if(num ===1){
return res;
}
res *= num;
return factorial(--num, res);
}
var factorial = function(num, res){
let res = Array.from(arguments)[1] || 1;
console.log(res);
if(num ===1){
return res;
}
res *= num;
return factorial(--num, res);
}
//tail-end
var factorial = function(num){
if(num === 1){
return num;
}
return num * factorial(--num);
}