This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' '); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 = () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// input: ['tea','you', 'eat', 'ate', 'ouy', 'hard'] | |
// output: [ | |
// ['tea', 'eat', 'ate'], | |
// ['you', 'ouy'], | |
// ['hard'] | |
// ] | |
function groupAnagram(arr) { | |
// we will put result here | |
const output = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
OlderNewer