Skip to content

Instantly share code, notes, and snippets.

View Arieg419's full-sized avatar

Omer Goldberg Arieg419

View GitHub Profile
@Arieg419
Arieg419 / index.html
Created September 17, 2017 19:35
Web Cookies for Everyone example
<!DOCTYPE html>
<html>
<head>
<script>
const setCookie = (key,val,days_till_expiration) => {
let d = new Date();
d.setTime(d.getTime() + (days_till_expiration*24*60*60*1000));
const expires = "expires=" + d.toGMTString();
document.cookie = key + "=" + val + ";" + expires + ";";
}
@Arieg419
Arieg419 / imperative.js
Last active November 27, 2017 07:44
Examples of imperative code
// triple the value of every element in a given array
const triple = (arr) => {
let results = []
for (let i = 0; i < arr.length; i++){
results.push(arr[i] * 3)
}
return results
}
// sum all the elements in a given array
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
const obj = {name: 'JP'}
const errorLoggerFunction = (err) => console.error(err);
jsonfile.writeFile(file, obj, errorLoggerFunction)
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
const obj = {name: 'JP'}
jsonfile.writeFile(file, obj, (err) => console.error(err))
const timeout = () => {
setTimeout(() => alert("WoW"), 1000);
}
const addWrapper = () => (x,y) => x + y;
const add = addWrapper();
const sum1 = add (1,2); // 3
// Or we could do it like this
const sum2 = addWrapper()(4,4); // 8
async function getUserToken(id) {
const token = await getTokenFromServer(id);
return token;
}
const functionAsObjectProperty = {
print: (value) => console.log(value)
};
functionAsObjectProperty.print("mic check"); // "mic check"
const add = (x,y) => x + y;
const subtract = (x,y) => x - y;
const multiply = (x,y) => x * y;
const arrayOfFunctions = [add, subtract, multiply];
arrayOfFunctions.forEach(calculationFunction => console.log(calculationFunction(1,1))); // 2 0 1
let a = 4;
let b = 5;
let c = 6;
const updateTwoVars = (a, b, c) => [b++, a * b];
const updateRes = updateTwoVars(a,b,c);
b = updateRes[0]
c = updateRes[1]