Skip to content

Instantly share code, notes, and snippets.

View andrit's full-sized avatar

Andrew Ritter andrit

View GitHub Profile
let firstList = [4, 6, 8, 9]
let secondList = [2, 3, 5, 7]

function getSmallestThenRemove(firstList, secondList) {
    let smallestFirstList = firstList[0];
    let smallestSecondList = secondList[0];

    if (smallestFirstList < smallestSecondList) {
 return firstList.shift()

Write a JSON to a file in Node

Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.

const fs = require('fs');
const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2))
// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'
function* repeatedArray(arr) {
  let index = 0;
  while (true) {
    yield arr[index++ % arr.length];
  }
}

const lifts = ['squat', 'bench', 'deadlift', 'press'];
const nextLiftGenerator = repeatedArray(lifts);
onSubmit = () => {
  return new Promise((resolve,reject) => {
    try{
    // try code here
    console.log('I am in the promise function!');
    resolve('resolved message');
    }
    catch(e){
 reject(e);
const pipe = functions => data => {
  return functions.reduce(
    (value, func) => func(value),
    data
    );
}

let cart = [3.12, 45.15, 11.01];
const addSalesTax = (total, taxRate) =&gt; (total * taxRate) + total;
function mapConsecutive(values, fn) {
  let result = [];
  for(let i=0; i < values.length - 1; i++){
    result.push(fn(values[i], values[i+1]));
   }
   return result;
}

const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
function logAndReturn(func) {
  return function() {
    var args = Array.prototype.slice.call(arguments);
    var result = func.apply(null, args);
    console.log('Result', result);
    return result;
  }
}

use apply to spread out arrays of values as parameters to a function call

function foo(a, b){
console.log( "a:" + a ", b:" + b );
}

foo.apply(null, [2, 3]);  //a:2, b:3

React component form field update of nested state for any text field in form

this.state={
user:{
  name: '',
  age: '',
  
}
}
function getBase64(file) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () {
     console.log(reader.result);
   };
   reader.onerror = function (error) {
     console.log('Error: ', error);
 };