Skip to content

Instantly share code, notes, and snippets.

function isExist(target, array) {
let sorted = array.sort((a, b) => a - b);
let midPoint = Math.floor(sorted.length / 2);
if (sorted[midPoint] === target) {
console.log('exist');
} else if (sorted.length <= 1) {
console.log('not exist');
} else if (sorted[midPoint] < target) {
isExist(target, sorted.slice(midPoint, sorted.length));
@muhsalaa
muhsalaa / findFirstDupe.js
Created October 18, 2020 00:33
find first duplicate character in string
function findDupe(str) {
let hash = {}
for (let x of str) {
hash[x] ? hash[x] += 1 : hash[x] = 1;
if (hash[x] === 2) return x
}
}
@muhsalaa
muhsalaa / misuh.js
Created December 2, 2020 13:11
Bag of words of dirty word in indonesia and other language
// simple function to check duplicate exist
function dupe(arr) {
return [...new Set(arr)].length === arr.length
}
// BOW
const misuh_warning = [
'asu','4su','45u','a5u',
]
@muhsalaa
muhsalaa / TenLeetCode.js
Last active March 2, 2021 03:49
answer of 10 leetcode challenge
/**
* 58. Length of last word
* https://leetcode.com/problems/length-of-last-word/
*/
const lengthOfLastWord = s => {
s = s.trim().split(' ');
const word = s[s.length - 1];
return word.length;
};
// ref https://codepen.io/Pestov/pen/BLpgm
const tree = {
root: {
root1: { name: "Kakek", deceased: true },
root2: { name: "Nenek", deceased: false },
childs: [
{
name: "Anak 1",
spouse: [{ name: "Istri anak 1", deceased: false, divorced: true }],
@muhsalaa
muhsalaa / findhidden.js
Created June 27, 2022 03:20
find all hidden element
function findAllHidden() {
const all = document.getElementsByTagName('*');
const hidden = [];
for (let i = 0; i < all.length; i++) {
if (window.getComputedStyle(all[i]).display === 'none') {
hidden.push(all[i]);
}
}
return hidden;
@muhsalaa
muhsalaa / AccessibleModal.tsx
Last active September 5, 2024 03:35
Creating an accessible modal with RadixUI like API
"use client";
import { X } from "lucide-react";
import React, { useEffect } from "react";
import { createPortal } from "react-dom";
import ReactFocusLock from "react-focus-lock";
export default function AccessibleDialogOrModal() {
return (
<>