Skip to content

Instantly share code, notes, and snippets.

Variables

const shade = 100;
type Shade = 100;

Functions

@q3e
q3e / System Design.md
Created January 25, 2020 12:59 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@q3e
q3e / cashDrawer.js
Last active February 3, 2017 11:50
Cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
function checkCashRegister(price, cash, cid) {
var change = cash - price, // The change due
count = 0,
denomArr = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100],
denominations = 9, // Number of denominations is a constant, check denomArr.length below
changeArr = [],
// Cash Drawer variables:
bestFactor = 0,
bestDenom = 0,
indexBestDenom = 0,
@q3e
q3e / mathMode.js
Last active January 8, 2017 07:45
Given array of integers, find its mode.
//This operation runs for 0.79ms - twice as previous version. PASSES for ALL array inputs.
//input expected is:
/* 3 ≤ sequence.length ≤ 5 · 10^4,
*1 ≤ sequence[i] ≤ 1000
*/
// NB revision, used Array.prototype.reduce()
function arrayMode(sequence) {
var countArr = [],
mod, count =0,
_seq = sequence.slice(0),
@q3e
q3e / isMAC48.js
Created January 6, 2017 13:30
The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).
//doesnt work yet
function isMAC48Address(inputString) {
MAC = inputString.split('-');
var regExChar = new RegExp('^[a-zA-Z]'),
regExNum = new RegExp('^[a-zA-Z]'),
bool = false;
console.log(MAC);
if(MAC.length === 6){
MAC.forEach(function(elem,idx,array){
if(elem.length === 2){
@q3e
q3e / symetricDifference.js
Last active December 24, 2016 08:20
A function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays.[FCC Symmetric Difference Challenge](https://www.freecodecamp.com/challenges/symmetric-difference)
function sym(args) {
var argsArr = Array.from(arguments),
arrLen = argsArr.length,
x = [],
//function to get symmetric Difference of 2 arrays
symDiffOfTwo = function(arr1, arr2){
//set default value of arr2 - es5
arr2 = arr2 || [] ;
arr1 = arr1 || [] ;
var diff = arr2.slice(0),
//works but needs improvements
function telephoneCheck(str) {
var isUSPhoneNum = false,
strArr = str.split(''),
numOfBracs = strArr.join('').split('(').length -1,
numsOnly = str.replace(/-|\s|[{()}]|[^0-9\-]/g,""),
numsLen = numsOnly.length,
firstNum = numsOnly.charAt(0),
firstParenth = str.indexOf('('),
secondParenth = str.indexOf(')'),
@q3e
q3e / argumentsOptional.js
Last active December 25, 2016 10:00
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum... https://www.freecodecamp.com/challenges/arguments-optional
function addTogether() {
var args1 = arguments[0],//1st arg to the function
args2 = arguments[1];//2nd arg to the function
if(typeof args1 === 'number'){
//both arguments are numbers
if(typeof args2 === 'number'){
return args1 + args2;
}
//one argument missing
if(args2 === undefined){
@q3e
q3e / everythingBeTrue.js
Last active December 13, 2016 14:52
Check if the predicate (second argument) is truthy on all elements of a collection (first argument). https://www.freecodecamp.com/challenges/everything-be-true
function truthCheck(collection, pre) {
var x = {};
var count = 0;
var len = collection.length;
for(var i=0;i<len;i++){
var prop = collection[i][pre];
/*a JS val like prop evals to true if its not a null,undefined,NaN,(""),0,false*/
if(collection[i].hasOwnProperty(pre)&& prop){
count++;
}
@q3e
q3e / binaryAgents.js
Created December 13, 2016 14:18
Return an English translated sentence of the passed binary string. The binary string will be space separated. https://www.freecodecamp.com/challenges/binary-agents
function binaryAgent(str) {
var arr = [];
//seperate array elems between spaces
str = str.split(' ').map(Number)
//convert elems from binary to decimals
.map( function( num ){ return (parseInt(num, 2));});
//convert binary string into English!
str.forEach(function(code){
arr.push(String.fromCharCode(code));
});