Skip to content

Instantly share code, notes, and snippets.

@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;
// 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 / 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;
};
@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 / 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
}
}
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));
// with first convert to string
function isPalindrome(int) {
if (int < 0) return false
return +(''+int).split('').reverse().join('') === int
}
// without convert to string (bukan aku yang kerjain, this is brilliant)
function isPalindrome(int){
if(int<0) return false;
let reverse=0;
@muhsalaa
muhsalaa / sockPairs.js
Created September 26, 2020 08:18
find sock pairs between socks array
function sockMerchant(n, ar) {
// create color hash
const colors = {};
// count total matches (pairs)
let matches = 0;
ar.forEach(x => {
// if color with truthy value exist, increment pairs by 1 and set color to zero to make it falsy
if (colors[x]) {
matches++;
colors[x] = 0
@muhsalaa
muhsalaa / mailer.js
Created September 23, 2020 13:53
nodemailer setup, work for yandex and google
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const transporter = nodemailer.createTransport(
smtpTransport({
service: 'yandex/google',
host: 'smtp.yandex.ru/smtp.gmail.com',
auth: {
user: 'your@email.com',
pass: 'yourEmailPassword',
@muhsalaa
muhsalaa / highlighter.js
Created September 19, 2020 06:25
function to get number need to be highlighted.
// question at https://muhsalaa.com/journal/3ab005b6-2c66-4ccc-b7bb-df1be5ff8777
function highlight(nominal, uniqueCode) {
// convert number to string
const totalString = String(nominal + uniqueCode);
const nominalString = String(nominal);
let count = 0;
// loop to count similar character by comparing nominal and total (nominal+unique code)
for (let x = 0; x < totalString.length; x++) {