Skip to content

Instantly share code, notes, and snippets.

@mustafadalga
mustafadalga / insertion-sort.ts
Created October 30, 2025 10:12
Insertion sort algorithm
function insertionSort(arr: number[]) {
for (let i = 1; i < arr.length; i++) {
const val = arr[i]
let j = i - 1
while (j >= 0 && arr[j] > val) {
arr[j + 1] = arr[j]
j--
}
@mustafadalga
mustafadalga / selectionSort.ts
Created October 28, 2025 15:50
Selection sort is a sorting algorithm that repeatedly scans an unsorted array and with each iteration finds the minimum element to build up a sorted array.
function selectionSort(arr) {
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
const temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
@mustafadalga
mustafadalga / bubbleSort.js
Created October 28, 2025 15:49
Bubble sort is a simple sorting algorithm which compares and swaps adjacent elements such that after every iteration over the array, one more element will be ordered/correctly placed, starting from the largest.
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
let swapped = false
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] >= arr[j + 1]) {
swapped = true
const temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
@mustafadalga
mustafadalga / AccordionWithTransition.tsx
Created October 28, 2025 07:30
React Accordion with Smooth Transition
import { useEffect, useRef, useState } from "react";
const list = [
{
id: "1",
title: "HTML",
context: "The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser."
},
{
id: "2",
@mustafadalga
mustafadalga / dynamic-property-creation.ts
Created October 26, 2025 14:21
Dynamic property creation | Proxy api
const target = {}
const handler = {
get(target: Record<string, any>, property: string) {
if (!Object.hasOwn(target, property)) {
target[property] = `Dynamic property ${property}`;
}
return target[property]
}
@mustafadalga
mustafadalga / proxy-function-caching.js
Created October 26, 2025 14:14
Demonstrates how to use JavaScript’s Proxy API to intercept function calls via the apply trap and implement simple caching (memoization) logic.
const map = new Map()
const target = async (property: string) => {
const random = (Math.random() * (2000 - 100)) + 100
await new Promise(resolve => setTimeout(resolve, random))
console.log(`fetch ${property} detail`)
return Date.now()
}
const handler = {
apply: async (target: (property: string) => Promise<number>, thisArg: unknown, argumentList: [ string ]): Promise<number> => {
@mustafadalga
mustafadalga / stringToZigzagGrid.ts
Created October 22, 2025 19:48
Convert a string into a 2D array representing its zigzag pattern for given number of rows.
function stringToZigzagGrid(text: string, numRows: number): string[][] | string {
if (numRows < 2) return text
const grid: string[][] = Array.from({ length: numRows }, () => [])
let row = 0, col = 0, direction = "down"
for (let index = 0; index < text.length; index++) {
grid[row][col] = text[index]
@mustafadalga
mustafadalga / cancelLinkedinOldInvitations.js
Created January 6, 2025 09:56
Linkedin Cancel All Old Invitations
async function cancelOldInvitations() {
const list = document.querySelectorAll(".invitation-card.artdeco-list__item");
const old = [
"Sent 1 months ago", "Sent 2 months ago", "Sent 3 weeks ago",
"Sent 2 weeks ago",
"Sent 3 months ago", "Sent 4 months ago", "Sent 5 months ago"
];
for (let row of [...list]) {
@mustafadalga
mustafadalga / createStore.ts
Created November 10, 2024 16:03
Building a Custom JavaScript State Management Library Inspired by Zustand
interface User {
_id: string;
name: string;
}
interface State {
users: User[];
addUser: (user: User) => void;
@mustafadalga
mustafadalga / App.tsx
Created April 1, 2024 20:35
Modal Transition Effect in React.js
import { lazy, Suspense, useEffect } from "react";
import useModalStore from "@/hooks/useModalStore.ts";
const Modal = lazy(() => import("@/components/Modal.tsx"))
function Loading() {
return <h2>🌀 Loading...</h2>;
}
function App() {