This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reduce(arr, reduceCallback, initialValue) { | |
if (!Array.isArray(arr) || !arr.length || typeof reduceCallback !== 'function') | |
{ | |
return []; | |
} else { | |
let hasInitialValue = initialValue !== undefined; | |
let value = hasInitialValue ? initialValue : arr[0]; | |
for (let i = hasInitialValue ? 0 : 1, len = arr.length; i < len; i++) { | |
value = reduceCallback(value, arr[i], i, arr); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function filter(arr, filterCallback) { | |
if (!Array.isArray(arr) || !arr.length || typeof filterCallback !== 'function') | |
{ | |
return []; | |
} else { | |
let result = []; | |
for (let i = 0, len = arr.length; i < len; i++) { | |
if (filterCallback(arr[i], i, arr)) { | |
result.push(arr[i]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function map(arr, mapCallback) { | |
if (!Array.isArray(arr) || !arr.length || typeof mapCallback !== 'function') { | |
return []; | |
} else { | |
let result = []; | |
for (let i = 0, len = arr.length; i < len; i++) { | |
result.push(mapCallback(arr[i], i, arr)); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [ ...Array(100).keys() ].map( i => i+1).sort((a, b) => a-b); | |
console.log(...arr) | |
var results = [] | |
while (arr.length > 0) { | |
results.push(arr.shift()) | |
results.push(arr.pop()) | |
} | |
console.log(...results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const delay = t => new Promise(resolve => setTimeout(resolve, t)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
t1 MEMORY t2 | |
0 | |
read 0 0 | |
inc 1 | |
write 1 1 | |
read 1 | |
inc 2 | |
write 2 2 | |
read 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constructor | |
function Car( model, year, miles ) { | |
this.model = model; | |
this.year = year; | |
this.miles = miles; | |
this.toString = function () { | |
return this.model + " has done " + this.miles + " miles"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reducer(acc, currentValue, currentIndex, arr) { | |
acc[currentValue] = (acc[currentValue]+1) || 1; | |
return acc; | |
} | |
function countWords(arrOfWords) { | |
return arrOfWords.reduce(reducer, {}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMap extends Map { | |
constructor() { | |
super(); | |
this.counter = 0; | |
this.all_value = null; | |
} | |
set(key, value) { | |
if (value) { | |
super.set(key, { ts: this.counter++, v: value }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const exp = '10+2*5*3+6'; | |
const result = exp.split('+').reduce((added, e) => added += (e.split('*').reduce((multiplied, k) => multiplied *= parseInt(k), 1) ), 0 ); | |
console.log(result); |