Skip to content

Instantly share code, notes, and snippets.

@isabellachen
isabellachen / underscore-underline-each-context.js
Last active April 3, 2018 09:42
Explaining 'bind to the context' with Underscore's each (underline excercise)
const each = function (collection, iteratee, context) {
if (Array.isArray(collection)) {
for (let [i, el] of collection.entries()) {
iteratee.call(context, el, i, collection)
}
} else {
for (let key in collection) {
if (collection.hasOwnProperty(key)) {
iteratee.call(context, collection[key], key, collection)
}
@isabellachen
isabellachen / toy-redux.js
Last active April 2, 2018 08:11
toy redux store with middlewares
let state = { names: [] }
const reducer = (state, action) => {
switch (action.type) {
case 'ADD_NAME':
return {
...state,
names: [
...state.names,
action.name
@isabellachen
isabellachen / permutations.js
Created April 3, 2018 08:57
Find all permutations of an array
function findPermutations (arr) {
let results = []
if (arr.length === 1) {
//base case, if arr === [3] there is only one permutation so permutations is [[3]]
return [arr]
}
for (let i=0; i<arr.length; i++) {
//seperate the pivot from the numbers to permute
let copy = arr.slice()
let pivot = copy.splice(i, 1)
@isabellachen
isabellachen / habitability-of-worlds.md
Last active April 8, 2018 10:42
Async Actions Redux Challenge

A challenge to get started with Redux Async Actions

Start off by getting up to speed with the docs.

Challenge

Build an app that calculates the habitability of 5 different astronomical bodies .

Here are the specifications:

@isabellachen
isabellachen / try-catch-promise-chain.md
Created April 8, 2018 09:50
Try/catch is redundant in promise chains

Clarification on how to handle errors thrown in promise chains

This was put together after some confusion in how to handle errors in the model. Refer to try catch in promise

try/catch is redundant in promise chains and promise executor functions. Any error thrown is automatically converted to a rejection of the promise you're supposed to return. The promise code calling your function takes care of this.

const asyncValidate = values =&gt; new Promise(resolve =&gt; {
@isabellachen
isabellachen / sumSquares.js
Created April 10, 2018 21:42
code wars: square into squares
//https://www.codewars.com/kata/square-into-squares-protect-trees/train/javascript
function sumSquares (n) {
function decompose (num, whatsLeft, result) {
if (whatsLeft === 0) return result
while (num * num > whatsLeft) num -= 1 //decrement within the same recursive level until a suitable target is found, this ensures num does not exceed the size of the call stack
if (num === 0) return null
//explore a path in the recursive tree from num-1, assuming num is a result (right)
//if we hit null, pop out to the previous level/s, explore the other path for num-1 where num is not a result
return decompose(num-1, whatsLeft - num * num, [num].concat(result)) || decompose(num-1, whatsLeft, result)
@isabellachen
isabellachen / merge-branch-into-develop.md
Last active April 11, 2018 10:52
git guide: merge feature branch into develop

EGSnrc development occurs on feature branches, which upon review are merged into the develop branch. The develop branch is not officially released, but many users rely on it to work with the latest features, hence commits to develop should be considered permanent (or else corrected immediately).

Preparing to merge a feature branch into develop involves the following steps:

  1. Open a pull request to merge a feature branch into the develop branch.
  2. Request reviews and wait for approvals.
  3. Force-push any required changes to the feature branch (updating the pull request).
    git push --force-with-lease
@isabellachen
isabellachen / http-server-ip.md
Last active April 16, 2018 14:17
Allow Webpack dev server to be accessed externally - set up simple http server
@isabellachen
isabellachen / syncing-a-fork.md
Last active April 18, 2018 20:20
syncing a fork, git, pair programming

Your pair and you have forked a repo. You pair program on your pair's computer. At the end of the day, she pushes the changes to her github repo. You want to pull those changes into your local repo, and then push them to your github repo.

First, configure a remote that points to the upstream (your pair's) repo in git.

Then fetch the changes from the original repo: git fetch upstream

git checkout <branchname> your fork's local branch you want to rebase the new changes made to the original repo.

Then, rebase.

@isabellachen
isabellachen / moment-sandbox.js
Last active April 18, 2018 15:02
moment js sandbox
//looks like momentJs takes care of daylight savings.
var winterTime = moment('2018-04-18T07:46:15Z').format('YYYY-MM-DD h:mm:ss')
var summerTime = moment('2018-03-02T08:53:47Z').format('YYYY-MM-DD h:mm:ss')
var sixMonthsAgo = moment().subtract(6, 'months').calendar()
console.log('-------dates--------')
console.log(winterTime)
console.log(summerTime)