This file contains hidden or 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
// 5.1 Flattening | |
let arrays = [[1, 2, 3], [4, 5], [6]]; | |
console.log(arrays.reduce((array1, array2) => array1.concat(array2))); | |
// → [1, 2, 3, 4, 5, 6] |
This file contains hidden or 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
// 5.2 looping version | |
function loop(value, test, update, body) { | |
for (let i = value; test(i); i = update(i)) { | |
body(i) | |
} | |
} | |
loop(3, n => n > 0, n => n - 1, console.log); | |
// → 3 | |
// → 2 |
This file contains hidden or 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
// 5.3 Everything | |
//every using a loop | |
function every(array, test) { | |
for (let element of array) { | |
if (test(element) === false) { | |
return false; | |
} | |
} | |
return true; | |
} |
This file contains hidden or 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
// 5.4 Dominant Writing Direction | |
function characterScript(code) { | |
for (let script of SCRIPTS) { | |
if (script.ranges.some(([from, to]) => code >= from && | |
code < to)) { | |
return script; | |
} | |
} | |
return null; |
This file contains hidden or 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 Vec { | |
constructor (x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
plus(v) { | |
return new Vec(this.x + v.x, this.y + v.y); | |
} | |
minus(v) { | |
return new Vec(this.x - v.x, this.y - v.y); |
This file contains hidden or 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 Group { | |
constructor() { | |
this.group = []; | |
} | |
add(item) { | |
if (!this.group.includes(item)) { | |
this.group.push(item); | |
} | |
} | |
delete(item) { |
This file contains hidden or 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 Group { | |
constructor() { | |
this.group = []; | |
} | |
add(item) { | |
if (!this.group.includes(item)) { | |
this.group.push(item); | |
} | |
} | |
delete(item) { |
This file contains hidden or 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
let map = {one: true, two: true, hasOwnProperty: true}; | |
// Fix this call | |
//console.log(map.hasOwnProperty("one")); | |
console.log(hasOwnProperty.call(map, 'one')); | |
// → true |
This file contains hidden or 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 runRobot(state, robot, memory) { | |
for (let turn = 0;; turn++) { | |
if (state.parcels.length == 0) { | |
return turn; | |
} | |
let action = robot(state, memory); | |
state = state.move(action.direction); | |
memory = action.memory; | |
} | |
} |
This file contains hidden or 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 runRobot(state, robot, memory) { | |
for (let turn = 0; ; turn++) { | |
if (state.parcels.length == 0) { | |
return turn; | |
} | |
let action = robot(state, memory); | |
state = state.move(action.direction); | |
memory = action.memory; | |
} | |
} |