Skip to content

Instantly share code, notes, and snippets.

View paulohfev's full-sized avatar
👊

Paulo Henrique Evangelista paulohfev

👊
View GitHub Profile
@paulohfev
paulohfev / quicksort-recursion.js
Created April 24, 2025 18:44
Quicksort using recursion
function quicksort(list) {
if (list.length < 2) {
return list
} else {
let pivot = list[0]
let restOfList = list.slice(1)
let listGreaterThanPivot = []
let listSmallerThanPivot = []
for (let i = 0; i < restOfList.length; i++) {
@paulohfev
paulohfev / calc-factorial.js
Created April 14, 2025 14:24
Calculate factorial with recursion
// Calculate factorial via recursion
const calculateFactorial = (num) => {
if (num === 1) {
return 1
} else {
return num * calculateFactorial(num - 1)
}
}
// console.log('3', calculateFactorial(3))
@paulohfev
paulohfev / selection-sort.js
Last active April 8, 2025 14:26
Selection sort algorithm
// Selection Sort
const sort = (arr) => {
let list = arr
const findSmallestValue = () => {
let smallest = list[0]
let smallestIndex = 0
for (let i = 1; i < list.length; i++) {
if (list[i] < smallest) {
@paulohfev
paulohfev / binary-search.js
Created April 7, 2025 00:59
Binary Search Algorithm
// Binary search
const bSearch = (list, number) => {
// low and high keep track of which part of the list you’ll search in.
let low = 0
let high = list.length - 1
// While you haven’t narrowed it down to one element …
while (low <= high) {
let mid = Math.floor((low + high) / 2)
// … check the middle element.
@paulohfev
paulohfev / find_max.js
Created April 4, 2023 00:28
Function for finding the highest number in a list of numbers
function find_max(nums) {
let max_num = Number.NEGATIVE_INFINITY; // smaller than all other numbers
for (let num of nums) {
if (num > max_num) {
max_num = num
}
}
return max_num;
}
@paulohfev
paulohfev / minimum-webpack.config.js
Last active October 8, 2022 20:03
Minimum webpack config for React-Typescript project
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.tsx",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
resolve: {
@paulohfev
paulohfev / formatCamelCase.js
Last active September 17, 2022 16:45
Format string in camel case
const NON_SPECIAL_CHARS_REGEX = /\W+|[_]+/;
const WHITE_SPACE_REGEX = /\s+/;
const formatCamelCase = (text) => {
const formatCase = (word, index) => {
const formattedNonFirstWord = word.charAt(0).toUpperCase() + word.slice(1);
return index === 0 ? word.toLowerCase() : formattedNonFirstWord
};
return text
.replace(NON_SPECIAL_CHARS_REGEX, ' ')
@paulohfev
paulohfev / html-regexes
Created July 19, 2022 17:05
List of regexes for matching HTML related tags, attributes, properties, etc.
const FIRST_PARAGRAPH_TAG_REGEX = /<p>(.*?)<\/p>/;
const INLINE_STYLE_REGEX = /style="[^"]*"/g;
@paulohfev
paulohfev / file-type-regexs.js
Created July 16, 2022 22:11
File type RegExs
const VIDEO_FILE_REGEX = /video\/\w+/gm;
const AUDIO_FILE_REGEX = /audio\/\w+/gm;
const IMAGE_FILE_REGEX = /image\/\w+/gm;
@paulohfev
paulohfev / useKeyDown.ts
Last active October 24, 2024 18:24
useKeyDown hook w/ TypeScript
import { useEffect } from 'react';
import { KeyboardKey } from 'enums/keyboardKey';
export const useKeyPress = (callback: (T?: any) => void, keys: KeyboardKey[]) => {
const onKeyDown = (event: KeyboardEvent) => {
const wasAnyKeyPressed = keys.some((key) => event.key === key);
if (wasAnyKeyPressed) {
event.preventDefault();
callback();