Skip to content

Instantly share code, notes, and snippets.

View davidkayce's full-sized avatar
🖐️
Busy

David Ojimba davidkayce

🖐️
Busy
View GitHub Profile
@davidkayce
davidkayce / javascript-proxy-as-rest-client.js
Created February 8, 2022 06:35 — forked from DavidWells/javascript-proxy-as-rest-client.js
Using a javascript proxy as low code REST client
/* Using a JavaScript proxy for a super low code REST client */
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3
const createApi = (url) => {
return new Proxy({}, {
get(target, key) {
return async function(id = "") {
const response = await fetch(`${url}/${key}/${id}`)
if (response.ok) {
return response.json();
@davidkayce
davidkayce / react-tic-tac-toe.jsx
Last active February 1, 2022 07:56
This is a sample tic tac toe application. Based on a coderbyte test
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const squareStyle = {
'width': '60px',
'height': '60px',
'backgroundColor': 'white',
'margin': '4px',
'display': 'flex',
@davidkayce
davidkayce / elevator.ts
Last active February 8, 2022 06:38
Elevator Engineering Test. Full test description can be found here: https://docs.google.com/document/d/1Vx2iVzvoB_wQ-fG5RC-tQviseJvs-mzofL_rAaz4t_A/edit?usp=sharing
interface IElevator {
on: (events: string, callback: Function) => void;
getCurrentFloor: () => number;
getCurrentDirection: () => DIRECTIONS;
moveUp: () => void;
moveDown: () => void;
}
interface IElevatorDoor {
open: () => void;
// A decorator is a function that can be used to modify/change different properties or methods in a class
// They are used inside/on classes only
// Understanding the orde that they are run is important in using decorators
// Decorators can be used on properties, methods or accessors(getters and setters);
// First argument is the prototype
// Second argument is the key ( the method, accessor or property)
// Third argument is the property descriptor
// We want to use a decorator factory as such that takes in externat arguments
const Logging = (message: string) => {
// 1. Find sum of even numbers from n
function findSumEvenNumbers(num) {
let n = 0;
if(even(num)){
n = num/2
} else {
n = (num - 1)/2
}
return n*n + n
}
/* Graph Search Algoroithms
* Here we would implement a graph in the form of an adjacency list
* There are a few ways this can be done :
* a.Adjacency Matrix: Represent each node in a 2D matrix, the edges represent relationships between nodes
* b.Adjacency List: Start with a collection of nodes each having their array of edges, this is easier 0on memmory and faster
* Adjacency matrix method is not desirable because adding, finding or working with nodes take an O(n^2) space and time complexity
*/
// Let's work with a graph connecting airports and their routes
const airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' ');
// Debounce: Debouncing enforces that a function not be called again until
// a certain amount of time has passed without it being called
// use:
// If we want to debounce an add function: const add = (a, b) => a + b;
// resulting debounce is debounce(add, 500)(2,3);
// this would give 5 only after 500ms of no input
export const debounce = (func, wait) => {
let timeout;
return (...args) => {
// This script progressively reduces the opacity of a site depending on a set number of days
// handly when you do a project and they refuse to pay you
(function () {
const dueDate = new Date('2020-02-12'); // Put in date of submission to client
const payDeadline = 30; // number of days as deadline for payment
const currentDate = new Date();
let utc1 = Date.UTC(dueDate.getFullYear(), dueDate.getMonth(), dueDate.getDate());
let utc2 = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
// Safe await
// This is from a brilliant article written by David Wells on a cleaner way to maange async operations in javascript
// here: https://davidwells.io/blog/cleaner-async-await-code-without-try-catch
/* Native Error types https://mzl.la/2Veh3TR */
const nativeExceptions = [
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
].filter((except) => typeof except === 'function')
// Queue using two stacks
function createDoubleStackQueue() {
var that = {};
var pushContainer = [];
var popContainer = [];
function moveElementToPopContainer() {
while (pushContainer.length !==0 ) {
var element = pushContainer.pop();