Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / chunkString.js
Created December 11, 2015 14:30
Split a string into chunks
"abcdefghijklmnopqrstuvwxyz".match( /.{1,4}/g );
// [ "abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx", "yz" ]
@Dammmien
Dammmien / shuffle.js
Created December 14, 2015 14:59
Shuffle an array with the Fisher–Yates shuffle in javascript
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;
}
@Dammmien
Dammmien / concat_and_dedupe.js
Last active December 15, 2015 17:00
Concatenate two arrays and remove duplicate
// 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;
}, [] );
@Dammmien
Dammmien / random_hex.js
Created December 14, 2015 17:04
Random Hex Color in JavaScript
'#' + Math.floor( Math.random() * 16777215 ).toString( 16 );
@Dammmien
Dammmien / hex_and_rgb_converter.js
Created December 14, 2015 17:31
RGB to Hex and Hex to RGB
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 ]
@Dammmien
Dammmien / dijkstra.js
Last active April 8, 2018 21:30
Implementation of Dijkstra's algorithm in JavaScript
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
},
@Dammmien
Dammmien / dfs.js
Last active February 22, 2022 11:37
Depth First Search (DFS) Graph Traversal in Javascript
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
},
...
];
@Dammmien
Dammmien / factorial.js
Created December 15, 2015 17:09
Factorial
var factorial = ( i, a ) => i < 2 ? a || 1 : factorial( i - 1, ( a || 1 ) * i );
console.log( factorial( 5 ) ); // 120
@Dammmien
Dammmien / count.js
Last active September 30, 2016 09:09
Counting the occurrences of elements in an array.
var arr = "The quick brown fox jumps over the lazy dog".split( '' );
arr.reduce( ( countMap, item ) => {
countMap[ item ] = ( countMap[ item ] || 0 ) + 1;
return countMap;
}, {} );
@Dammmien
Dammmien / rgbToLab.js
Created February 5, 2016 10:39
RGB to LAB
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 );