Skip to content

Instantly share code, notes, and snippets.

View dengjonathan's full-sized avatar

Jon Deng dengjonathan

View GitHub Profile
@dengjonathan
dengjonathan / puke.js
Last active September 12, 2016 03:40
Puke function in lines 3-5 allows you to see props passed to a React component without actually having to create the UI
var React = require('react');
var PropTypes = React.PropTypes;
var styles = require('../styles');
var Link = require('react-router').Link
var UserDetails = require('../components/UserDetails');
var UserDetailsWrapper = require('../components/UserDetailsWrapper');
function puke(object) {
return <pre>{JSON.stringify(object, null, ' ')}</pre>;
}
import React from "react";
import { render } from "react-dom";
const ParentComponent = React.createClass({
getDefaultProps: function() {
console.log("ParentComponent - getDefaultProps");
},
getInitialState: function() {
console.log("ParentComponent - getInitialState");
return { text: "" };
@dengjonathan
dengjonathan / Example2_Solution.js
Created September 10, 2016 21:54
map function
names2 = ['kendra', 'kim', 'kampbell'];
// use this as your callback function in map
var capitalize = function(word) {
return word[0].toUpperCase() + word.slice(1);
};
// we need each implemented for map to work
var each = function(array, callback) {
for (var i = 0; i < array.length; i++) {
@dengjonathan
dengjonathan / add.js
Created September 10, 2016 19:07
pure function
var d = 'not modified';
// add is a pure function that doesn't modify anything out of its scope
function add(x, y) {
return x + y;
}
add(2, 3) // 5
add(2, 3) // always gonna be 5
console.log(d) // 'not modified'
@dengjonathan
dengjonathan / callToMap.js
Created September 10, 2016 18:32
callToMap
names2 = ['kendra', 'kim', 'kampbell'];
var capitalize = function(word) {
return word[0].toUpperCase() + word.slice(1);
};
map(names, capitalize); // ['Kendra', 'Kim', 'Kampbell']
@dengjonathan
dengjonathan / map.js
Last active September 10, 2016 18:58
Naive implementations of each and map
names2 = ['kendra', 'kim', 'kampbell'];
// use this as your callback function in map
var capitalize = function(word) {
return word[0].toUpperCase() + word.slice(1);
};
// we need each implemented for map to work
var each = function(array, callback) {
for (var i = 0; i < array.length; i++) {
@dengjonathan
dengjonathan / forLoop.js
Created September 10, 2016 17:43
for loop
var i;
var names = ['jon', 'jane', 'jack'];
// imperative programming
for (i = 0; i < names.length; i++) {
names[i] = names[i][0].toUpperCase() + names[i].slice(1);
}
console.log(names) // ['Jon', 'Jane', 'Jack']
console.log(i) // 3
@dengjonathan
dengjonathan / forLoop.js
Created September 10, 2016 17:40
using for loop to iterate through array
var i;
var names = ['jon', 'jane', 'jack'];
// imperative programming
for (i = 0; i < names.length; i++) {
names[i] = names[i][0].toUpperCase() + names[i].slice(1);
}
console.log(names) // ['Jon', 'Jane', 'Jack']
console.log(i) // 3
@dengjonathan
dengjonathan / plusOne.js
Created September 10, 2016 17:14
shows difference between a pure function and not pure
// this is better
function plusOne(number) {
return number++;
}
plusOne(5); // 6
// this is worse
var number = 5;
function plusOne() {
@dengjonathan
dengjonathan / each.js
Last active September 10, 2016 21:56
replaces for loop with each function
/* how do you encapsulate a for loop within a function
so that each(names, callback) will do the same thing as the for loop? */
// ensure that the all inputs are explicity declared in function signature
var each = function(array, callback) {
for (var i = 0; i < array.length; i++) {
callback(array[i]);
}
};