Step Between Two Numbers
copy const stepBetweenA = ( min, max, steps) => Array ( steps) .fill ( 0 ) .reduce ( ( prev, curr, i) => { prev.push ( min + ( ( max - min) / ( steps - 1 ) ) * i) return prev } , [ ] ) const stepBetweenB = ( min, max, steps) => { steps -= 1 const diff = ( max - min) / steps const result = [ min] for ( let i = 0 ; i < steps; i++ ) { result.push ( min += diff) } return result } console.log ( 'a' , stepBetweenA( 10 , 110 , 4 ) ) console.log ( 'b' , stepBetweenB( 10 , 110 , 4 ) ) const ITER = 10000 console.time ( 'a' ) for ( let i = 0 ; i < ITER; i++ ) { stepBetweenA( 10 , 110 , 4 ) } console.timeEnd ( 'a' ) console.time ( 'b' ) for ( let i = 0 ; i < ITER; i++ ) { stepBetweenB( 10 , 110 , 4 ) } console.timeEnd ( 'b' )
Try it out…
Two messy implementations for stepping between two numbers… I am not sure it’s possible to make console.time
work well with the snippet zone quick editor – so if you want to see the times… open your console.
Hamming Distance in JavaScript
copy function hamming( a, b) { const leng = a.length let dist = 0 // strings need to be same length if ( leng != b.length ) return - 1 ; a = a.toLowerCase ( ) b = b.toLowerCase ( ) for ( let i = 0 ; i < leng; i++ ) if ( a[ i] !== b[ i] ) dist++ return dist } console.log ( hamming( 'zevan' , 'kevin' ) ) console.log ( hamming( 'joe' , 'joe' ) ) console.log ( hamming( 'john' , 'jake' ) )
Try it out…
The hamming distance between two strings…
Fake RNG
copy let anchorslet idxlet leng = 10 let size = 200 let px = 0 let py = 0 function seed( ) { idx = 0 anchors = ( Date .now ( ) + '' ) .split `` .reverse ( ) .map ( v => parseFloat( v) / 10 ) .splice ( 0 , leng) } let last = 0 let zoom = 1 function rand( ) { if ( idx > size * size) seed( ) px += zoom py += ~~( px / size) if ( px >= size) px = 0 if ( py >= size) py = 0 const point = { x: anchors[ idx % leng] , y: anchors[ ( idx + 1 ) % leng] } idx++ let dists = [ ] for ( let i = 0 ; i < anchors.length ; i += 2 ) { let dx = px - anchors[ i] * size let dy = py - anchors[ i + 1 ] * size dists.push ( Math .sqrt ( dx * dx + dy * dy) ) } dists.sort ( ) last += ( dists[ 0 ] / size - last) / 4 return last } seed( ) let d = documentlet b = d.body with ( b.appendChild ( Object .assign ( d.createElement `canvas`, { width: 400 , height: 400 } ) ) .getContext `2d`) { fillStyle = 'black' fillRect( 0 , 0 , 400 , 400 ) for ( let i = 0 ; i < 200 ; i++ ) { for ( let j = 0 ; j < 200 ; j++ ) { const c = rand( ) * 255 fillStyle = `rgb( ${ c} , ${ c} , ${ c} ) ` fillRect( j * 2 , i * 2 , 1 , 2 ) } } }
Try it out…
Another one for genuary “Create your own pseudo-random number generator and visually check the results.”
Raphaël Easing Equations
copy var ef = R.easing_formulas = { linear: function ( n) { return n; } , "<" : function ( n) { return pow( n, 1.7 ) ; } , ">" : function ( n) { return pow( n, .48) ; } , "<>" : function ( n) { var q = .48 - n / 1.04 , Q = math.sqrt ( .1734 + q * q) , x = Q - q, X = pow( abs( x) , 1 / 3 ) * ( x < 0 ? - 1 : 1 ) , y = - Q - q, Y = pow( abs( y) , 1 / 3 ) * ( y < 0 ? - 1 : 1 ) , t = X + Y + .5; return ( 1 - t) * 3 * t * t + t * t * t; } , backIn: function ( n) { var s = 1.70158 ; return n * n * ( ( s + 1 ) * n - s) ; } , backOut: function ( n) { n = n - 1 ; var s = 1.70158 ; return n * n * ( ( s + 1 ) * n + s) + 1 ; } , elastic: function ( n) { if ( n == !! n) { return n; } return pow( 2 , - 10 * n) * math.sin ( ( n - .075) * ( 2 * PI) / .3) + 1 ; } , bounce: function ( n) { var s = 7.5625 , p = 2.75 , l; if ( n < ( 1 / p) ) { l = s * n * n; } else { if ( n < ( 2 / p) ) { n -= ( 1.5 / p) ; l = s * n * n + .75; } else { if ( n < ( 2.5 / p) ) { n -= ( 2.25 / p) ; l = s * n * n + .9375; } else { n -= ( 2.625 / p) ; l = s * n * n + .984375; } } } return l; } } ; ef.easeIn = ef[ "ease-in" ] = ef[ "<" ] ; ef.easeOut = ef[ "ease-out" ] = ef[ ">" ] ; ef.easeInOut = ef[ "ease-in-out" ] = ef[ "<>" ] ; ef[ "back-in" ] = ef.backIn ; ef[ "back-out" ] = ef.backOut ;
Another fun chunk of code directly from the Raphaël source. Makes me think of the Penner easing equations .