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
traverse(root, word) { | |
if(root.isLeaf) { | |
this.suggestions.push(word); | |
return; | |
} | |
for(const letter in root.children) { | |
this.traverse(root.children[letter], word + letter); | |
} | |
} |
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
find(word) { | |
let root = this.trie; | |
for(const letter of word) { | |
if(letter in root.children) { | |
root = root.children[letter]; | |
} | |
else { | |
return null; // if not found return null | |
} | |
} |
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
this.trie = { | |
isLeaf: false, | |
children: { | |
'a': { | |
isLeaf: false, | |
children: { | |
'n': { | |
isLeaf: false, | |
children: { | |
'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
newNode() { | |
return { | |
isLeaf: false, | |
children: {} | |
} | |
} | |
add(word) { | |
if(!this.trie) this.trie = this.newNode(); |
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 Trie { | |
constructor() { | |
this.trie = null; | |
this.suggestions = []; | |
} | |
} |
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
let dummy1 = 10; | |
console.log(window.dummy1) // undefined | |
const dummy2 = 10; | |
console.log(window.dummy2) // undefined | |
const f = () => { | |
console.log("THIS DOESN'T WORK"); // TypeError: window.f is not a function |
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 obj = { | |
prop: 'Hello', | |
method: function() { | |
'use strict' | |
function inner() { | |
console.log(this === undefined); // true | |
console.log(this.prop + ', World!'); // TypeError: Cannot read property 'prop' of undefined | |
} | |
inner(); |
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 obj = { | |
prop: 'Hello', | |
method: function() { | |
setTimeout(() => { | |
console.log(this.prop + ', World!'); // Hello, World! | |
}, 2000); | |
} | |
}; | |
obj.method(); |
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 obj = { | |
prop: 'Hello', | |
method: function() { | |
function inner() { | |
console.log(this.prop + ', World!'); // Hello, World! | |
} | |
inner.call(this); | |
} | |
}; |
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 obj = { | |
prop: 'Hello', | |
method: function() { | |
const callback = function() { | |
console.log(this.prop + ', World!'); // Hello, World! | |
} | |
setTimeout(callback.bind(this), 2000); | |
} | |
}; |