Skip to content

Instantly share code, notes, and snippets.

View CodHeK's full-sized avatar
🤙
Existing

Gagan Ganapathy CodHeK

🤙
Existing
View GitHub Profile
traverse(root, word) {
if(root.isLeaf) {
this.suggestions.push(word);
return;
}
for(const letter in root.children) {
this.traverse(root.children[letter], word + letter);
}
}
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.trie = {
isLeaf: false,
children: {
'a': {
isLeaf: false,
children: {
'n': {
isLeaf: false,
children: {
't': {
newNode() {
return {
isLeaf: false,
children: {}
}
}
add(word) {
if(!this.trie) this.trie = this.newNode();
class Trie {
constructor() {
this.trie = null;
this.suggestions = [];
}
}
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
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();
const obj = {
prop: 'Hello',
method: function() {
setTimeout(() => {
console.log(this.prop + ', World!'); // Hello, World!
}, 2000);
}
};
obj.method();
const obj = {
prop: 'Hello',
method: function() {
function inner() {
console.log(this.prop + ', World!'); // Hello, World!
}
inner.call(this);
}
};
const obj = {
prop: 'Hello',
method: function() {
const callback = function() {
console.log(this.prop + ', World!'); // Hello, World!
}
setTimeout(callback.bind(this), 2000);
}
};