Last active
May 1, 2019 00:00
-
-
Save completejavascript/9256765561609f12b9bf06f3aa713c73 to your computer and use it in GitHub Desktop.
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => { | |
let r = null; | |
switch(type) { | |
case 1: r = f1(...params); break; | |
case 2: r = f2(...params); break; | |
case 3: r = f3(...params); break; | |
default: console.log("Unknown type"); | |
} | |
return r; | |
} | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => params.reduce((sum, curr) => sum + curr, 0); | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => [f1, f2, f3].filter(f => f.length === type)[0](...params) | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => eval(`f${type}`)(...params); | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => { | |
let sum = 0; | |
[...params].forEach((p, index) => { | |
if (index >= type) return; | |
sum += p; | |
}); | |
return sum; | |
} | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => [f1, f2, f3][type - 1](...params); | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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 f1 = a => a; | |
const f2 = (a, b) => a + b; | |
const f3 = (a, b, c) => a + b + c; | |
const func = (type, ...params) => { | |
let r = null; | |
switch(type) { | |
case 1: r = f1.apply(null, params); break; | |
case 2: r = f2.apply(null, params); break; | |
case 3: r = f3.apply(null, params); break; | |
default: console.log("Unknown type"); | |
} | |
return r; | |
} | |
func(1, 1); // -> 1 (1) | |
func(2, 1, 2); // -> 3 (1 + 2) | |
func(3, 1, 2, 3);// -> 6 (1 + 2 + 3) |
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
/* | |
* Định dạng lại số thập phân 'number', | |
* với số lượng chữ số thập phân sau dấu phẩy | |
* tối đa là 'maxDecimal' | |
*/ | |
const format = (number, maxDecimal) => { | |
const pd = Math.pow(10, maxDecimal); | |
return Math.round(pd * number) / pd; | |
} | |
console.log(format(1, 2)); // => 1; | |
console.log(format(1.1, 2)); // => 1.1; | |
console.log(format(1.123, 2)); // => 1.12; | |
console.log(format(1.125, 2)); // => 1.13; |
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
/* | |
* Định dạng lại số thập phân 'number', | |
* với số lượng chữ số thập phân sau dấu phẩy | |
* tối đa là 'maxDecimal' | |
*/ | |
const format = (number, maxDecimal) => { | |
return parseFloat(number.toFixed(maxDecimal)); | |
} | |
console.log(format(1, 2)); // => 1; | |
console.log(format(1.1, 2)); // => 1.1; | |
console.log(format(1.123, 2)); // => 1.12; | |
console.log(format(1.125, 2)); // => 1.13; |
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 toHex = (r, g, b) => { | |
const format = (v) => v.toString(16).padStart(2, "0"); | |
const rr = format(r); | |
const gg = format(g); | |
const bb = format(b); | |
return `#${rr}${gg}${bb}`; | |
} | |
console.log(toHex(0, 255, 255)); // => #00ffff | |
console.log(toHex(255, 0, 255)); // => #ff00ff | |
console.log(toHex(255, 255, 0)); // => #ffff00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment