Skip to content

Instantly share code, notes, and snippets.

@lei-clearsky
Last active December 19, 2018 13:59
Show Gist options
  • Select an option

  • Save lei-clearsky/c46e05756874ab86a0fe5659f5ab4066 to your computer and use it in GitHub Desktop.

Select an option

Save lei-clearsky/c46e05756874ab86a0fe5659f5ab4066 to your computer and use it in GitHub Desktop.
// JS Hoisting
function parent() {
var hoisted = "I'm a variable";
function hoisted() {
return "I'm a function";
}
return hoisted();
}
console.log(parent());
// JS Inheritance
// Explain
// JS Scoping, Closure
// interviewer: what will the following code output?
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('Index: ' + i + ', element: ' + arr[i]);
}, 3000);
}
// ANSWER
// Index: 4, element: undefined(printed 4 times).
// The reason for this is because the setTimeout function creates a function (the closure) that has access to its outer scope,
// which is the loop that contains the index i. After 3 seconds go by, the function is executed and it prints out the value of
// i, which at the end of the loop is at 4 because it cycles through 0, 1, 2, 3, 4 and the loop finally stops at 4.arr[4] does
// not exist, which is why you get undefined.
// JS Event Loop
(function() {
console.log('this is the start');
setTimeout(function cb() {
console.log('this is a msg from call back');
});
console.log('this is just a message');
setTimeout(function cb1() {
console.log('this is a msg from call back1');
}, 0);
console.log('this is the end');
})();
// "this is the start"
// "this is just a message"
// "this is the end"
// note that function return, which is undefined, happens here
// "this is a msg from call back"
// "this is a msg from call back1"
// Algo 1
// Create a dictionary
// dictionary('One Punch Man');
// return { o: 1, n: 3, e: 1, p: 1, u: 1, c: 1, h: 1, m: 1, a: 1 }
function dictionary() {
}
// dictionary('One Punch Man');
// Solution
function dictionary(str) {
let dict = {};
const formattedStr = str.replace(/\s/g, '').toLowerCase();
for (let i = 0; i < formattedStr.length; i++) {
const currentChar = formattedStr.charAt(i);
dict[currentChar] = dict[currentChar] ? ++dict[currentChar] : 1;
}
return dict;
}
// Algo 2
function reverseString() {}
// Algo 3
// Balanced Parenthesis
// Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is valid.
// The brackets must close in the correct order, () and ()[]{} are all valid but (] and ([)] are not.
// Algo 4
const liftOff = (n) => {
if (n === 0) {
return "Lift off"
} else {
console.log(n);
liftOff(n-1);
console.log(n);
}
}
// JS
/*
* Create an event emitter that goes like this
* emitter = new Emitter();
*
* Allows you to subscribe to some event
* emitter.subscribe('event1', callback1);
* (you can have multiple callbacks to the same event)
* emitter.subscribe('event1', callback2);
*
* You can emit the event you want with this api
* (you can receive 'n' number of arguments)
* sub1.emit('event1', foo, bar);
*/
function Emitter() {
this.events = {};
}
Emitter.prototype.subscribe = function(eventName, callback) {
if (this.events[eventName]) {
this.events[eventName].push(callback);
} else {
this.events[eventName] = [callback];
}
}
Emitter.prototype.emit = function(eventName) {
if (this.events[eventName]) {
var args = Array.prototype.slice.call(arguments, 1);
this.events[eventName].forEach(function(callback) {
callback.apply(null, args);
});
}
}
// tests
function callback1(arg1, arg2) {
console.log('callback1 ', arg1);
console.log('callback1 ', arg2);
}
function callback2(arg1, arg2) {
console.log('callback2 ', arg1);
console.log('callback2 ', arg2);
}
var emitter = new Emitter();
emitter.subscribe('func1', callback1);
emitter.subscribe('func1', callback2);
emitter.emit('func1', 'foo', 'bar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment