This file contains hidden or 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
"abcdefghijklmnopqrstuvwxyz".match( /.{1,4}/g ); | |
// [ "abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx", "yz" ] |
This file contains hidden or 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 shuffle( a ) { | |
var m = a.length, t, i; | |
while ( m ) { | |
i = Math.floor( Math.random() * m-- ); | |
t = a[ m ]; | |
a[ m ] = a[ i ]; | |
a[ i ] = t; | |
} |
This file contains hidden or 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
// if no duplicate in the first list | |
a1.concat( a2.filter( i => a1.indexOf( i ) === -1 ) ); | |
// if duplicate in the first list | |
a1.concat( a2 ).reduce( function( p, c, i, a ) { | |
if ( p.indexOf( c ) === -1 ) p.push( c ); | |
return p; | |
}, [] ); |
This file contains hidden or 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
'#' + Math.floor( Math.random() * 16777215 ).toString( 16 ); |
This file contains hidden or 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
var rgbToHex = ( r, g, b ) => "#" + ( ( 1 << 24 ) + ( r << 16 ) + ( g << 8 ) + b ).toString( 16 ).slice( 1 ); | |
var hexToRgb = ( hex ) => [ parseInt( hex.substring( 1, 3 ), 16 ), parseInt( hex.substring( 3, 5 ), 16 ), parseInt( hex.substring( 5, 7 ), 16 ) ]; | |
rgbToHex( 175, 25, 70 ); // #af1946 | |
hexToRgb( "#af1946" ); // [ 175, 25, 70 ] |
This file contains hidden or 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
var nodes = [ | |
{ | |
links: [ 1, 3 ], // node 0 is linked to node 1 and 3 | |
weightLinks: [ 5, 15 ], // the distance between node 0 and 1 is 5, between 0 and 3 is 15 | |
distance: Infinity | |
}, { | |
links: [ 0, 2 ], // node 1 is linked to node 0 and 2 | |
weightLinks: [ 5, 5, ], // the distance between node 1 and 0 is 5, between 1 and 2 is 5 | |
distance: Infinity | |
}, |
This file contains hidden or 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 nodes = [ | |
{ | |
links: [ 1 ], // node 0 is linked to node 1 | |
visited: false | |
}, { | |
links: [ 0, 2 ], // node 1 is linked to node 0 and 2 | |
visited: false | |
}, | |
... | |
]; |
This file contains hidden or 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
var factorial = ( i, a ) => i < 2 ? a || 1 : factorial( i - 1, ( a || 1 ) * i ); | |
console.log( factorial( 5 ) ); // 120 |
This file contains hidden or 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
var arr = "The quick brown fox jumps over the lazy dog".split( '' ); | |
arr.reduce( ( countMap, item ) => { | |
countMap[ item ] = ( countMap[ item ] || 0 ) + 1; | |
return countMap; | |
}, {} ); |
This file contains hidden or 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 rgbToLab( R, G, B ) { | |
R = ( R / 255 ); | |
G = ( G / 255 ); | |
B = ( B / 255 ); | |
if ( R > 0.04045 ) R = Math.pow( ( R + 0.055 ) / 1.055, 2.4 ); | |
else R = R / 12.92; | |
if ( G > 0.04045 ) G = Math.pow( ( G + 0.055 ) / 1.055, 2.4 ); | |
else G = G / 12.92; | |
if ( B > 0.04045 ) B = Math.pow( ( B + 0.055 ) / 1.055, 2.4 ); |