Skip to content

Instantly share code, notes, and snippets.

@Jiert
Jiert / Sidebar.js
Last active May 8, 2017 16:46
How to use hashed with Redux
// URL hash should be the keeper of its own state. In this example we're using the "comparing" state,
// i.e. are we in compare mode or not. Since the toggle button lives in the sidebar, we would do something
// like this in the sidebar component:
getInitialState() {
comparing: false
}
componentWillMount() {
this.updateHash = hashed.register({comparing: false}, this.onHashChange);
@Jiert
Jiert / stringify.js
Created February 2, 2017 21:12
Stringify
// Implement JSON.stringify
var example = {
"array": [1, 2, 3],
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
@Jiert
Jiert / alien-dictionary.js
Last active January 12, 2017 15:54
Alien Dictionary
var words = [
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
var alphabet = []
var hash = {};
@Jiert
Jiert / composition-object-assign.js
Last active April 14, 2020 23:42
Composition using factory functions and Object.assign()
var runner = function(){
return {
run: function(){
console.log(this.name + ' is running away!');
}
};
};
var swimmer = function(){
return {
@Jiert
Jiert / factory-composition.js
Created October 5, 2015 18:43
Composition using factory functions
var barker = function(state){
return { bark: function(){ console.log('Woof, I am ' + state.name); } };
};
var driver = function(state){
return { drive: function(){
state.position = state.position + state.speed;
console.log(state.position);
} };
};
@Jiert
Jiert / composition.js
Created October 5, 2015 18:22
Composition Example
const barker = (state) => ({
bark: () => console.log('Woof, I am ' + state.name)
})
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
})
barker({ name: 'karo'}).bark()
// Woof, I am karo
@Jiert
Jiert / factory.js
Last active October 5, 2015 18:22
ES6 Factory Function example
const dog = () => {
const sound = 'woof'
return {
talk: () => console.log(sound)
}
}
const sniffles = dog()
sniffles.talk() // "woof"
@Jiert
Jiert / this.js
Last active August 28, 2015 04:14
Code from an interview question about 'this'
var foo = {
a: function () {
console.log(this);
}
};
foo.a();
// => foo
foo['a']();
@Jiert
Jiert / eval.js
Created August 25, 2015 20:01
I found this in a Shopify theme.
var data = eval('(' + XMLHttpRequest.responseText + ')');
@Jiert
Jiert / converter.js
Created August 25, 2015 04:38
An interview test: Convert a string "1234" into an integer 1234 without using parseInt or Number
function converter(st){
var arrSt = st.split(''),
arrInt = [];
for (var i = 0; i < arrSt.length; i++){
arrInt.push(arrSt[i].charCodeAt() - 48);
}
// loop over arry again