Skip to content

Instantly share code, notes, and snippets.

@iHani
iHani / arrayMethods.js
Last active August 6, 2018 20:10
Example of array methods: forEach, map, filter, some, every, indexOf, and include. (ES2015 syntax)
/*
* forEach
*/
// forEach is very simlar to for-looping arr.length
// array.forEach(callback, context)
var arr = ['apple', 'orange', 'watermelon', 10, 20, 30]
arr.forEach((value, index) => console.log(`Element's ${index} type is ${typeof value}`))
// Prints:
// Element 0 type is string
@iHani
iHani / classes.js
Last active August 6, 2018 20:10
Example of classes in JavaScript ES2015
class Person {
constructor(name = 'Anonymous', age = 0) {
this.name = name
this.age = age
}
getGreeting(){
return `Hi, I am ${this.name}`
}
getDescription(){
return `${this.name} is ${this.age} years old`
@iHani
iHani / Promises.js
Last active August 4, 2018 22:58
Promises in JavaScript with examples
/*
* ** Diffrence between Tasks and Promises:
* Once settled, a promise can not be resettled. Calling resolve() or reject() again will have no effect. The immutability of a settled promise is an important feature.
* https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261
* *** First arg is for 'resolved' status, and the second is for 'rejected'
*/
// exampe 1
const myPromise = function () {
return new Promise((Yay, Nay) => {
@iHani
iHani / activity.py
Created October 11, 2017 17:07 — forked from scturtle/activity.py
undocumented twitter activity api
# coding: utf-8
import oauth2 as oauth
import json
CONSUMER_KEY = "yT577ApRtZw51q4NPMPPOQ"
CONSUMER_SECRET = "3neq3XqN5fO3obqwZoajavGFCUrC42ZfbrLXy5sCv8"
ACCESS_KEY = ""
ACCESS_SECRET = ""
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
@iHani
iHani / Destructuring.js
Last active August 6, 2018 20:11
Destructuring in ES2015 basic examples
/*
* Object Destructuring
*/
const book = {
title: 'ReWork',
author: 'Some guy',
publisher: {
name: 'Penguin'
}
}
@iHani
iHani / SpreadOperator.js
Last active June 3, 2020 19:33
Spread Operator
/*
* Array spread operator
*/
const arr = [1, 3, 5]
arr.concat(7)
// [1, 3, 5, 7] // returns a new array, doesn't change arr
arr.push(11)
// [1, 3, 5, 11] // actually changes arr
@iHani
iHani / hoc.js
Last active August 6, 2018 20:11
Higher Order Component (HOC) example with React
// Higher Order Component (HOC) : A component that renders another component
// Goal: Reduce code, render hijacking, prop manipulation, abstract state
// Inspired by mead.io videos on Udemy
import React from 'react'
import reactDOM from 'react-dom'
const info = (props) => (
<div>
<h1>Hello { props.isAdmin ? 'admin' : 'guest' }</h1>
@iHani
iHani / async-await.js
Last active August 4, 2018 22:52
async await basic example.
const isLargerOrEqualTo100 = (a) => {
return new Promise((resolve, reject) => {
if (a >= 100) {
resolve(`${a} is >= 100`)
} else {
reject(`${a} is < 100`)
}
})
}
@iHani
iHani / Currying.js
Last active August 4, 2018 22:56
Currying (also partial application)
function person (name) {
return function age (age) {
return `${name} is ${age}`
}
}
// if we called
const arg1 = 'Richard'
const arg2 = 30
console.log(person(arg1)(arg2))
@iHani
iHani / map_examples.js
Created November 10, 2018 05:15
array.map() examples- JavaScript
// "The map() method creates a new array with the results of calling a provided function on every element in the calling array." - MDN
// .map() returns an array of the same length, don't use it for filtering, use .filter() insted
/* example 1 */
const fruits = [ 'orange', 'apple', 'pineapple' ];
// maps are good for 'applying' something on every element in the array, let's apply .toUpperCase()
const fruitsUppercase = fruits.map(fruit => fruit.toUpperCase());
console.log(fruitsUppercase);
// [ 'ORANGE', 'APPLE', 'PINEAPPLE' ]