Skip to content

Instantly share code, notes, and snippets.

View frankstepanski's full-sized avatar
🏠
Working from home

Frank Stepanski frankstepanski

🏠
Working from home
View GitHub Profile
@frankstepanski
frankstepanski / app.js
Last active February 16, 2021 00:49
Basic Express Boilerplate
require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const helmet = require('helmet')
const { NODE_ENV } = require('./config')
const app = express()
const morganOption = (NODE_ENV === 'production')
@frankstepanski
frankstepanski / index.html
Last active February 16, 2021 00:48
jQuery-enabled Quiz App Game
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" name="description" content="Quiz App">
<title>Quiz App</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="main.css">
@frankstepanski
frankstepanski / fetch-async-await.js
Last active February 16, 2021 00:48
Using fetch API and async/await
// GET:
//set the specific API URL
const url = 'http://www.example.com/tasks';
const getFetch = async (url) => {
const response = await fetch(url);
//convert response to Json format
const myJson = await response.json();
// return the response
return myJson ;
}
@frankstepanski
frankstepanski / reduce.js
Created January 30, 2021 01:34
Reproducing the reduce() method
function reduce(arr, callbackFunc, initVal) {
/*
1. make sure array is being passed
2. if initVal is not passed, accumulator uses the first element in array
then just use the array starting with 2nd element
if initVal is passed, accumulator is set to initVal
*/
if (Array.isArray(arr)) {
let accum;
if (initVal === undefined) {
@frankstepanski
frankstepanski / map.js
Last active January 30, 2021 02:32
Reproducing the map() method
function subtractTwo(num) {
return num - 2
}
function map(arr, callbackFunc) {
const newArr = [];
for( let i = 0; i < arr.length; i++) {
newArr.push(callbackFunc(arr[i]));
}
return newArr;
@frankstepanski
frankstepanski / factorial.js
Last active January 30, 2021 02:32
Factorial algo using recursion
function factorial(num, product = 1) {
// base case: num === 1; return product
// 1! = 1 || 0! = 1
if (num <= 1) return product
// recursive case: factorial (num -1, num * product)
return factorial(num - 1, num * product);
}
console.log(factorial(4)); // -> 24
console.log(factorial(6)); // -> 720
@frankstepanski
frankstepanski / promise.md
Last active January 30, 2021 02:06
Understanding the syntax of a promise

Promises

A promise is a type of object that typically wraps around asynchronous code. Promises won't run this code until needed, and they provide methods to extract the final result.

Promises are an ingenious solution. They allow you to wrap your asynchronous code inside an object, and then they provide you with some tools so that you can respond to the asynchronous code when you want to.

To manage asynchronous code, promises have three states:

  • Pending: When a promise is first created, it has a status of pending. For example, with setTimeout(), the pending status would apply to the time that it takes for setTimeout() to be run.
  • Fulfilled: When the promise has finished running, it has a status of fulfilled. This means that it is ready to pass back a value. In the setTimeout() example, this is after the given number of milliseconds have been run.
@frankstepanski
frankstepanski / promise-all.md
Last active January 30, 2021 02:30
Promise.all

Promise.all

Chaining promises lets us make a series of requests, one after another, knowing that each request will finish before the next one starts. But what if we want to make multiple requests at the same time, then wait for all of them to finish before doing something else?

Promise.all() solves this problem, making it possible to send multiple requests and receive multiple responses at the same time, even if we don't know the exact number of requests ahead of time. This lets you display data from multiple requests more quickly to the user.

Promise.all() takes an array of promises as an argument, and returns a single promise that resolves when all the promises passed to Promise.all() have resolved. The fact that Promise.all() returns a promise means we can chain a .then() call after Promise.all() as follows:

const axios = require('axios');
@frankstepanski
frankstepanski / fetch.js
Created January 30, 2021 06:14
Using fetch API
// GET:
// no custom headers
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => console.error(error))
// custom headers:
@frankstepanski
frankstepanski / looping-over-objects.js
Last active March 9, 2022 02:26
JavaScript foundations: Looping over objects
const cart = {
"Gold Round Sunglasses": { quantity: 1, priceInCents: 1000 },
"Pink Bucket Hat": { quantity: 2, priceInCents: 1260 },
};
// The calculateCartTotal function will take in the cart and return a total price, in cents, of everything inside of it.
function calculateCartTotal(cart) {
let grandTotal = 0;
for (const prop in cart) {