Skip to content

Instantly share code, notes, and snippets.

View alishahlakhani's full-sized avatar
🤘
Busy Inventing the future

Ali Shah Lakhani alishahlakhani

🤘
Busy Inventing the future
View GitHub Profile
@alishahlakhani
alishahlakhani / find-lcm.ts
Created August 21, 2024 09:58
Algorithm // Finding LCM in Typescript using Euclidean algorithm
function findGDC(num1: number, num2: number): number {
function calcMod(v1: number, v2: number): number {
return v1 % v2;
}
let a = num1;
let b = num2;
while (b > 0) {
const mod = calcMod(a, b);
if (mod === 0) break;
@alishahlakhani
alishahlakhani / prime-factors.ts
Created August 21, 2024 09:55
Algorithm // Finding Prime Factors for a number // Inefficient
function isPrime(num: number): boolean {
return findFactors(num).length === 2;
}
function findFactors(num1: number): Array<number> {
let factors: Array<number> = [];
for (let index = 0; index <= num1; index++) {
if (num1 % index == 0) {
factors.push(index);
@alishahlakhani
alishahlakhani / random-fn.ts
Created August 21, 2024 09:29
Algorithms // Generate random number stream // From "Data Structures and Algorithms: From Zero to Hero" on Udemy
// Variable to keep track of old value
const A: number = 22695477;
const B: number = 12820163;
const M: number = Math.pow(2, 24);
let prev: number = 0;
function modGen(val: number, mod: number): number {
return val - Math.trunc(val / mod) * mod;
}
@alishahlakhani
alishahlakhani / finding-gcd.ts
Last active August 21, 2024 09:31
Algorithm // Finding Greatest Common Divisor in Typescript using Euclidean algorithm
function findGDC(num1: number, num2: number): number {
function calcMod(v1: number, v2: number): number {
return v1 % v2;
}
let a = num1;
let b = num2;
while (b > 0) {
const mod = calcMod(a, b);
if (mod === 0) break;
@alishahlakhani
alishahlakhani / madeofzero-nextjs-framer-flip-card.tsx
Last active August 23, 2024 14:22
Create a Tarot card deck selection page using Framer Motion and Nextjs
"use client";
import React, { useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import Image from "next/image";
import clsx from "clsx";
type Props = {
onPick?: (card: string | null) => void;
onSelect?: (card: string) => void;
};
@alishahlakhani
alishahlakhani / ID.js
Created February 11, 2021 07:35
ID - a unique ID/name generator for JavaScript
// Generate unique IDs for use as pseudo-private/protected names.
// Similar in concept to
// <http://wiki.ecmascript.org/doku.php?id=strawman:names>.
//
// The goals of this function are twofold:
//
// * Provide a way to generate a string guaranteed to be unique when compared
// to other strings generated by this function.
// * Make the string complex enough that it is highly unlikely to be
// accidentally duplicated by hand (this is key if you're using `ID`