Skip to content

Instantly share code, notes, and snippets.

function joinArrayRange(array, start, end, color, normalize) {
let result = '';
const joined = array.slice(start, end + 1).join(' ');
array.splice(start, end - start + 1, `<span style="background: ${color}">${joined}</span>`, ...[...Array(end - start).fill('<xxx>')]);
if (normalize) {
result = array.filter(item => item !== '<xxx>').join(' ');
} else {
result = array.join(' ');
}
@muhsalaa
muhsalaa / convertImageUrlToBase64.js
Created April 24, 2020 05:36
Simple promise based function to convert image url to base64 string
/**
* Convert a url of image to base64 string
* @param {string} imgUrl string of image url
*/
function convertBase64(imgUrl) {
return new Promise((resolve) => {
var img = new Image();
// onload fires when the image is fully loadded, and has width and height
img.onload = () => {
@muhsalaa
muhsalaa / groupAnagram.js
Last active May 18, 2020 04:55
Interview question about group anagram answer in javascript
// input: ['tea','you', 'eat', 'ate', 'ouy', 'hard']
// output: [
// ['tea', 'eat', 'ate'],
// ['you', 'ouy'],
// ['hard']
// ]
function groupAnagram(arr) {
// we will put result here
const output = {};
@muhsalaa
muhsalaa / HackerRank.js
Last active May 20, 2020 04:10
list of answer of hackerrank problem in javascript
// 1. https://www.hackerrank.com/challenges/counting-valleys/problem
// count how many valleys has been passed by hiker
function countingValleys(n, s) {
let valleys = 0;
let count = 0;
s.split('').forEach(x => {
if (x === 'U') {
count += 1
// check if count === 0 means it is the time when hiker go to sea level from valleys, valleys += 1
@muhsalaa
muhsalaa / handlingLargeFormField.js
Last active June 12, 2020 05:51
Handling large amount of iterated form field with react hooks
import React, { useState, useCallback, useRef, useEffect } from 'react';
export default function Multipleform() {
let [data, setData] = useState([...Array(5000).fill('')]);
// wrap handler with useCallback to keep its reference
// so React.memo will not consider it change overtime
const handler = useCallback((value = '', i = 0) => {
setData((state) => {
// return state modified with map to change value of certain index
@muhsalaa
muhsalaa / propchangeTracer.js
Last active July 13, 2022 03:35
Trace prop change in react component
function useTraceUpdate(props) {
const prev = React.useRef(props);
React.useEffect(() => {
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
if (prev.current[k] !== v) {
ps[k] = [prev.current[k], v];
}
return ps;
}, {});
if (Object.keys(changedProps).length > 0) {
@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++) {
@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: '[email protected]',
pass: 'yourEmailPassword',
@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
// 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;