• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const permutations = arr => { if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr; return arr.reduce( (acc, item, i) => acc.concat( permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [ item, ...val, ]) ), [] ); }; permutations([1, 33, 5]); // [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]
const formatDuration = ms => { if (ms < 0) ms = -ms; const time = { day: Math.floor(ms / 86400000), hour: Math.floor(ms / 3600000) % 24, minute: Math.floor(ms / 60000) % 60, second: Math.floor(ms / 1000) % 60, millisecond: Math.floor(ms) % 1000 }; return Object.entries(time) .filter(val => val[1] !== 0) .map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`) .join(', '); }; formatDuration(1001); // '1 second, 1 millisecond' formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
• Mar 11, 2021 •C S
1 like • 1 view
if (process.env.NODE_ENV === "production") { app.use(express.static("client/build")); app.get("*", (req, res) => { res.sendFile(path.resolve(__dirname, "client", "build", "index.html")); }); }
• Nov 18, 2022 •AustinLeath
0 likes • 1 view
// Or const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`; console.log(randomColor);
• Nov 8, 2023 •LeifMessinger
0 likes • 6 views
function copyString(text){ async function navigatorClipboardCopy(text){ if(document.location.protocol != "https:"){ return false; } return new Promise((resolve, reject)=>{ navigator.clipboard.writeText(text).then(()=>{resolve(true);}, ()=>{resolve(false);}); }); } function domCopy(text){ if(!(document.execCommand)){ console.warn("They finally deprecated document.execCommand!"); } const input = document.createElement('textarea'); input.value = text; document.body.appendChild(input); input.select(); const success = document.execCommand('copy'); document.body.removeChild(input); return success; } function promptCopy(){ prompt("Copy failed. Might have ... somewhere. Check console.", text); console.log(text); } function done(){ alert("Copied to clipboard"); } navigatorClipboardCopy(text).catch(()=>{return false;}).then((success)=>{ if(success){ done(); }else{ if(domCopy(text)){ done(); }else{ promptCopy(); } } }); }