Skip to content

Instantly share code, notes, and snippets.

function merge(left, right)
{
var result = [];
while (left.length && right.length) {
if (left[0] <= right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0)
var rightSum = A.reduce((p, c) => p+c)
var leftSum = 0;
var min = Number.MAX_SAFE_INTEGER;
for(var i = 0; i < A.length - 1; i++){
leftSum += A[i]
function fibonacci(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
@TimCodes
TimCodes / findsum.js
Created January 20, 2017 22:21
Detect if there is a pair of elements that equal a given sum in an unordered list
function test(arr, sum){
let isIn = false
arr.forEach( function(el){
let c = comps.indexOf(el)
if(c > -1)
isIn = true
}else{
comps.push(sum - el)
}
})
@TimCodes
TimCodes / counterReducer.js
Created February 13, 2017 22:20
simple counter reducer
const counter = (state, action) => {
// if state is not defined
// return default state
if(typeof state === 'undefined'){
return 0;
}
let type = action.type;
if(type === "INCREMENT"){
return state + 1;
@TimCodes
TimCodes / reduxStore.js
Created February 13, 2017 22:41
simplified version of redux create store
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
@TimCodes
TimCodes / index.html
Created February 14, 2017 19:30 — forked from anonymous/index.html
JS Bin [simple react counter] // source https://jsbin.com/rawefa
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[simple react counter]">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<link href="http://extjs.cachefly.net/ext-3.1.0/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
@TimCodes
TimCodes / index.html
Last active February 14, 2017 19:30 — forked from anonymous/index.html
simple reducers example
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[simple react counter]">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<link href="http://extjs.cachefly.net/ext-3.1.0/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
@TimCodes
TimCodes / combineReducers.js
Created February 15, 2017 19:19
simplified combine reducers
const combineReducers = (reducers) => {
return (state = {}, action) => {
return Object.keys(reducers).reduce(
(nextState, key) => {
nextState[key] = reducers[key](
state[key],
action
);
return nextState;
},
@TimCodes
TimCodes / todoReducer.js
Created February 16, 2017 00:00
todoReducerExample
const todo = (state, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {