Skip to content

Instantly share code, notes, and snippets.

@amitasaurus
amitasaurus / maxVowels.ts
Last active May 7, 2024 06:52
LeetCode 1456. Maximum Number of Vowels in a Substring of Given Length
function maxVowels(s: string, k: number): number {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
let maxVowels = 0;
let currVowels = 0;
for(let i = 0; i < k; i++){
if(vowels.has(s[i])) currVowels++
}
maxVowels = currVowels;
@amitasaurus
amitasaurus / parseJSON.ts
Created May 6, 2024 12:35
Write a function to convert json string to js object in javascript without using JSON.parse
function parseJSON(jsonString: string): Record<string, any> {
const result: Record<string, any> = {};
jsonString = jsonString.substring(1, jsonString.length - 1);
const pairs = jsonString.split(',');
for (const pair of pairs) {
const keyValue = pair.split(':');
let key = keyValue[0].trim();
let value = keyValue[1].trim();
key = key.substring(1, key.length - 1);
@amitasaurus
amitasaurus / curry.ts
Created May 5, 2024 09:46
Implementation of curry in TS
export default function curry(func: Function): Function {
return function curried(this: any, ...args: any[]) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function (this: any, ...args1: any[]) {
return curried.apply(this, args.concat(args1));
};
}
};
@amitasaurus
amitasaurus / throttle.ts
Created May 5, 2024 04:56
Implement a throttle function which accepts a callback function and a wait duration. Calling throttle() returns a function which throttled invocations of the callback function following the described behaviour.
/**
* Throttling is a technique used to control how many times we allow a function to be executed over time.
* When a JavaScript function is said to be throttled with a wait time of X milliseconds, it can only be invoked at most once every X milliseconds.
* The callback is invoked immediately and cannot be invoked again for the rest of the wait duration.
*/
type ThrottleFunction<T extends any[]> = (...args: T) => any;
export default function throttle<T extends any[]>(
func: ThrottleFunction<T>,
wait: number
@amitasaurus
amitasaurus / validParentheses.ts
Last active May 5, 2024 04:04
The validParentheses function takes a string containing various types of parentheses ((), {}, []) as input and checks whether the arrangement of parentheses is valid. It returns true if the input string contains a valid arrangement of parentheses, meaning that for every opening parenthesis, there is a corresponding closing parenthesis in the cor…
function validParentheses(str: string): boolean {
const stack: string[] = [];
const map: Record<string, string> = {
'}': '{',
')': '(',
']': '[',
};
for (let index = 0; index < str.length; index++) {
const char = str[index];
if (map[char]) {
@amitasaurus
amitasaurus / forwardRef.tsx
Created May 4, 2024 04:44
forwardRef simple syntax
interface MyComponentProps {
// Your component props here
}
const MyComponent = forwardRef<HTMLInputElement, MyComponentProps>(
(props, ref) => {
// Render your component with the ref attached to the desired element
return <input ref={ref} {...props} />;
}
);
@amitasaurus
amitasaurus / debounce.ts
Created May 4, 2024 02:56
implementation of debounce in TypeScript
function debounce(
func: (...args: any[]) => any,
delay: number
): (...args: any[]) => void {
let timeoutId: ReturnType<typeof setTimeout> | null;
return function debouncedFunction(...args: any[]): void {
clearTimeout(timeoutId!);
timeoutId = setTimeout(() => {
@amitasaurus
amitasaurus / Checklist.md
Created May 2, 2024 10:16
Payment and address form best practices
- [Use meaningful HTML elements](#meaningful-html): `<form>`, `<input>`, `<label>`, and `<button>`.
- [Label each form field with a `<label>`](#html-label).
- Use HTML element attributes to [access built-in browser features](#html-attributes), in particular `type` and `autocomplete` with appropriate values.
- Avoid using `type="number"` for numbers that aren't meant to be incremented, such as payment card numbers. Use `type="text"` and [`inputmode="numeric"`](#make_it_easy_for_users_to_enter_data) instead.
- If an [appropriate autocomplete value](#autocomplete-attribute) is available for an `input`, `select`, or `textarea`, you should use it.
- To help browsers autofill forms, give input `name` and `id` attributes [stable values](#checkout-forms:%7E:text=giving%20form%20fields-,name%20and%20id%20attributes,-stable%20values%20that) that don't change between page loads or website deployments.
- [Disable submit buttons](#html-button) once they've been tapped or clicked.
- [Validate](#validate) data d
@amitasaurus
amitasaurus / index.html
Created May 2, 2024 05:23
HTML5 Boilerplate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
@amitasaurus
amitasaurus / getElementsByClassName.ts
Created April 30, 2024 05:12
getElementsByClassName() is a method which exists on HTML Documents and Elements to return an HTMLCollection of descendant elements within the Document/Element which has the specified class name(s).
export default function getElementsByClassName(
element: Element,
classNames: string
): Array<Element> {
const elementsArr: Element[] = [];
function traverseNode(el: Element) {
if (el.hasChildNodes()) {
el.childNodes.forEach((node) => {
const nodeEl = node as Element;