Fibonacci Triangle Golfed
copy // by Arnauld - https://codegolf.stackexchange.com/users/58563/arnauld f= ( n, a= b= 1 , p) => n? '' .padEnd ( p) + a+ `+ ${ b} = ${ b+= a} `+ f( n- 1 , b- a, ( a+ "" ) .length - ~p) : '' console.log ( f( 20 ) )
Try it out…
Great golfed solution to this question at codegolf stackexchange by user Arnauld
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.”