Skip to content

Instantly share code, notes, and snippets.

View Giagnus64's full-sized avatar

Gianfranco Nuschese Giagnus64

View GitHub Profile
@Giagnus64
Giagnus64 / app.ts
Last active February 5, 2020 23:13
Interface with class implementation
type FieldPosition = 'GOALIE'|'STRIKER'|'MIDFIELD'|'DEFENDER'
interface PlayerConfig {
name: string;
age: number;
jerseyNumber?: (number | string)
}
//ERROR!!
const player1: PlayerConfig = {
name: "Jason",
@Giagnus64
Giagnus64 / app.ts
Last active February 5, 2020 04:16
TypeScript Object w/new types
enum FieldPosition {GOALIE, STRIKER, MIDFIELD, DEFENDER};
//pre-declared types
const player: {
name: 'Bob';
age: 24;
hobbies: string[];
//tuple
team: [string, number];
position: number;
active: any;
@Giagnus64
Giagnus64 / app.ts
Last active February 2, 2020 21:15
TypeScript Object + Array
// pre-declared types
const person: {
name: string;
age: number;
hobbies: string[];
} = {
name: 'Bob',
age: 24,
hobbies: ['books', 'music'],
}
@Giagnus64
Giagnus64 / app.ts
Last active February 2, 2020 20:00
TypeScript w/ proper typing
const numButton = document.querySelector(".add-numbers")! as HTMLButtonElement;
const strButton = document.querySelector(".add-with-string")! as HTMLButtonElement;
const num1Input = document.querySelector(".num1-input")! as HTMLInputElement;
const num2Input = document.querySelector(".num2-input")! as HTMLInputElement;
const strInput = document.querySelector(".string-input")! as HTMLInputElement;
function add(
thing1: number,
thing2: number,
phrase: string,
@Giagnus64
Giagnus64 / app.ts
Last active February 2, 2020 19:44
TypeScript w/ proper typing and errors
const numButton = document.querySelector(".add-numbers")! as HTMLButtonElement;
const strButton = document.querySelector(".add-with-string")! as HTMLButtonElement;
const num1Input = document.querySelector(".num1-input")! as HTMLInputElement;
const num2Input = document.querySelector(".num2-input")! as HTMLInputElement;
const strInput = document.querySelector(".string-input")! as HTMLInputElement;
const answer = document.querySelector(".answer")! as HTMLSpanElement;
function add(
thing1: number,
thing2: number,
@Giagnus64
Giagnus64 / app.ts
Last active February 2, 2020 20:01
TypeScript w/out typing
//WITHOUT TYPING
const numButton = document.querySelector(".add-numbers");
const strButton = document.querySelector(".add-with-string");
const num1Input = document.querySelector(".num1-input");
const num2Input = document.querySelector(".num2-input");
const strInput = document.querySelector(".string-input");
function add(thing1, thing2, phrase, showString){
return showString ? phrase + (thing1 + thing2) : thing1 + thing2
@Giagnus64
Giagnus64 / intermediate_sorts.rb
Created January 22, 2020 02:37
Merge, Quick and Radix sort in Ruby
# ASSUMES ARRAYS ARE SORTED
def merge(arr1, arr2)
sorted_array = []
first_pointer = 0
second_pointer = 0
while(first_pointer < arr1.size && second_pointer < arr2.size)
if(arr2[second_pointer] >= arr1[first_pointer])
sorted_array.push(arr1[first_pointer])
first_pointer += 1
@Giagnus64
Giagnus64 / radixSort.js
Created January 20, 2020 23:33
Radix Sort w/out helpers
const radixSort = (nums) => {
//get the max digits for the list
let maxDigits = mostDigits(nums);
//iterate from 0 to number of max digits
//digit iterator
for(let i = 0; i < maxDigits; i++){
// make digit buckets by crafting an array with 10 arrays inside it
let digitBuckets = Array.from({length:10}, () => [])// same as const digitBuckets = [[],[],[],[],[],[],[],[],[],[]]
//iterate over our numbers and place them into the proper bucket using helper functions
@Giagnus64
Giagnus64 / Radix Sort Helpers
Last active January 20, 2020 23:31
Radix Sort Helper Fucntions
//get the digit in requested place (working backwards i.e. ones place is the last digit, tens is the second to last and so on)
const getDigit = (num, place) => {
//move the decimal point over to the requested digit (absolute value to consider negative numbers)
const divided = Math.abs(num) / Math.pow(10, place);
//round down to eliminate digits after the decimal
const floored = Math.floor(divided);
//modulo to get remainder when number is divided by it's base (10)
const digit = floored % 10;
@Giagnus64
Giagnus64 / quickSort.js
Last active January 20, 2020 22:27
Quick Sort Implementation w/out helpers
//creating default arguments again since we want to be able to change them in later recursions
const quickSort = (arr, left = 0, right = arr.length - 1) => {
//if the array has more than 1 item
if(left < right){
//get the pivot index
let pivotIndex = pivot(arr, left, right);
//pass in the sort for the left side of the pivot
quickSort(arr, left, pivotIndex - 1);
//pass in the sort for the right side of the pivot
quickSort(arr, pivotIndex + 1, right);