• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed); // Examples toFixed(25.198726354, 1); // 25.1 toFixed(25.198726354, 2); // 25.19 toFixed(25.198726354, 3); // 25.198 toFixed(25.198726354, 4); // 25.1987 toFixed(25.198726354, 5); // 25.19872 toFixed(25.198726354, 6); // 25.198726
const head = arr => (arr && arr.length ? arr[0] : undefined); head([1, 2, 3]); // 1 head([]); // undefined head(null); // undefined head(undefined); // undefined
0 likes • 1 view
// There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9. // You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where: // The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B'). // The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9'). // For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1. // Return the number of rods that have all three colors of rings on them. let rings = "B0B6G0R6R0R6G9"; var countPoints = function(rings) { let sum = 0; // Always 10 Rods for (let i = 0; i < 10; i++) { if (rings.includes(`B${i}`) && rings.includes(`G${i}`) && rings.includes(`R${i}`)) { sum+=1; } } return sum; }; console.log(countPoints(rings));
const binomialCoefficient = (n, k) => { if (Number.isNaN(n) || Number.isNaN(k)) return NaN; if (k < 0 || k > n) return 0; if (k === 0 || k === n) return 1; if (k === 1 || k === n - 1) return n; if (n - k < k) k = n - k; let res = n; for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; return Math.round(res); }; binomialCoefficient(8, 2); // 28
• Mar 9, 2021 •LeifMessinger
alert("bruh")
// promisify(f, true) to get array of results function promisify(f, manyArgs = false) { return function (...args) { return new Promise((resolve, reject) => { function callback(err, ...results) { // our custom callback for f if (err) { reject(err); } else { // resolve with all callback results if manyArgs is specified resolve(manyArgs ? results : results[0]); } } args.push(callback); f.call(this, ...args); }); }; } // usage: f = promisify(f, true); f(...).then(arrayOfResults => ..., err => ...);