Nonsense Logs
copy const SPEED = 700 ; const addKeys = obj => { obj.keys = Object .keys ( obj) return obj } const wordParts = { start: addKeys( { z: .3, zim: .1, zz: .1, zzz: .1, ziz: .05, zoo: .1, koe: .05, kob: .05, 'cow cow' : .1, 'how ' : .02, sl: .1, ko: .05 } ) , mid: addKeys( { i: .1, oo: .1, a: .1, e: .3, oe: .1, ebobe: .1, y: .1 } ) , end: addKeys( { z: .3, n: .1, t: .05, rat: .1, '!' : .05, '?' : .05, '' : .1 } ) } ; const wordPart = ( type, dupeOdds = .1, dupeMax = 8 , capOdds = .05 ) => { let result = '' ; const part = wordParts[ type] ; while ( result === '' ) { const choice = part.keys [ Math .floor ( Math .random ( ) * part.keys .length ) ] ; if ( Math .random ( ) < part[ choice] ) { result = choice; if ( Math .random ( ) < dupeOdds) { const dupes = Math .round ( Math .random ( ) * Math .random ( ) * dupeMax ) ; result = result.repeat ( dupes) ; } } } wordPart.accum += result; if ( Math .random ( ) < capOdds) { wordPart.accum = wordPart.accum .toUpperCase ( ) } return wordPart; } wordPart.accum = '' ; const word = ( ) => wordPart( 'start' ) ( 'mid' ) ( 'end' ) .accum + ' ' document.body .style .fontSize = "2em" ; setInterval( ( ) => { document.body .innerHTML += word( ) ; wordPart.accum = '' } , SPEED) ;
Try it out…
Sometimes to entertain myself I will write nonsense style print/log statements. Things like console.log('cowcow')
. This snippet generates a variety of these kinds of phrases.
Random Hex Color (semi-golfed)
copy document.body .innerHTML += 'click anywhere...' onclick = ( ) => document.body .style .background = `#${ Math .random ( ) .toString ( 16 ) .substr ( - 6 ) } `
Try it out…
I golfed this snippet slightly for no reason in particular. I recently posted a nice readable way to make random hsl
colors. This snippet generates a random hexidecimal
color.
How it works:
copy Math .random ( ) // random number between 0 and 1 .toString ( 16 ) // convert to hex string (something like "0.2d6bcee4198d4") .substr ( - 6 ) // grab the last 6 characters
Here is a non-golfed version:
copy const instructionsEl = document.createElement ( 'p' ) ; instructionsEl.innerHTML = 'click anywhere...' ; document.body .appendChild ( instructionsEl) ; const randomHexColor = ( ) => `#${ Math .random ( ) .toString ( 16 ) .substr ( - 6 ) } `; document.addEventListener ( 'click' , ( ) => { document.body .style .background = randomHexColor( ) ; } ) ;
Try it out…
Proxies and Dynamic Methods
copy const spec = { get ( o, key) { console.log ( key, ':: key' ) ; return o[ key] != null ? o[ key] : o[ key] = ( ) => { document.body .innerHTML += `< div> ${ key} !</ div> `; return anyMethod( ) ; } } , set ( o, key, v) { o[ key] = v; } } ; const anyMethod = ( ) => new Proxy( { } , spec) ; anyMethod( ) .now ( ) .this ( ) .is ( ) .pretty ( ) .cool ( ) .confusing ( ) .evil ( ) ? .or ( ) .maybe ( ) .powerful ( ) [ '... what <b>do</b> <i>you</i><br>' ] ( ) .think ( ) ;
Try it out…
Proxies can be used for all manner of strange “magic”. I can actually see some uses for this, might post in the next few days…
Easy Random Color
copy document.body .innerHTML += 'click to randomize background color' ; document.addEventListener ( 'click' , ( ) => { const hue = Math .random ( ) * 360 ; document.body .style .background = `hsl( ${ hue} deg, 50 %, 50 % ) `; } ) ;
Try it out…
One way to generate a random color is to randomize the hue
argument of the css hsl
. This value is in degrees 0-360 (colorwheel). The other arguments can be randomized as well if you need random saturation and lightness… like this:
copy document.body .innerHTML += 'click to randomize background color' ; document.addEventListener ( 'click' , ( ) => { const hue = Math .random ( ) * 360 ; const saturation = Math .random ( ) * 100 ; const lightness = Math .random ( ) * 100 ; document.body .style .background = `hsl( ${ hue} deg, ${ saturation} %, ${ lightness} % ) `; } ) ;
Try it out…