Guess the semi-golf
a=3217+'';a=[a];console.log(a[0][2])
Try and guess what would be logged here…
a=3217+'';a=[a];console.log(a[0][2])
Try and guess what would be logged here…
x='hello'
x.replace(/h/g,'') // 17 bytes
x.split`h`.join`` // 16 bytes
x.replaceAll('h','') // 19 bytes
Another fun one from the great javascript golfing tips stackexchange thread.
This one comes from user emanresu-a and this answer.
const _100 = 100;
const count = i => {
document.body.innerHTML +=
_100.toString(i) + ` :: ... ${i}<br>`
i++ < 36 && count(i)
}
count(2)
Display 100 in many bases from binary to base 36… Today is the 100th post on Snippet Zone.
const aOrB = /\b(a|b)\b/g;
const fn = '(b > .5) ? (max(a, 2. * (b - .5))) : (min(a, 2. * b))'
const r = fn.replace(aOrB, '$1.r');
const g = fn.replace(aOrB, '$1.g');
const b = fn.replace(aOrB, '$1.b');
console.log(r);
console.log(g);
console.log(b);
Match an a
or b
, but not when it’s part of another word.
Found myself needing this for auto-generating some shaders recently.
const target = 'snippetzone'.split``;
const leng = target.length;
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split``;
function randomString() {
let str = [];
for (let i = 0; i < leng; i++) {
str.push(randomChar());
}
return str;
}
function randomChar() {
return alphabet[Math.floor(Math.random() * alphabet.length)];
}
let iterations = 0;
const search = randomString();
const indices = [];
for (var i = 0; i < leng; i++) indices.push(i);
let found = false;
function loop() {
for (let i = 0; i < 10; i++) {
if (indices.length > 0) {
let ii = Math.floor(Math.random() * indices.length);
let index = indices[ii];
search[index] = randomChar();
if (search[index] == target[index]) {
indices.splice(ii, 1);
}
console.log(search.join(','));
iterations++;
} else {
console.log('found after', iterations, 'iterations');
found = true;
break;
}
}
if (!found) {
requestAnimationFrame(loop);
}
}
loop();
This is a port of an old snippet of mine. It naively and randomly finds a target string. In this case the string “snippetzone”.
It’s fun to see it randomly perform better/worse…
e,f,k,w,q,s,o,h,n,r,f
e,f,k,w,q,s,o,h,n,r,u
k,f,k,w,q,s,o,h,n,r,u
k,f,k,w,q,o,o,h,n,r,u
k,f,k,w,q,o,o,v,n,r,u
k,f,k,w,q,v,o,v,n,r,u
...
s,e,i,p,p,e,t,z,o,n,e
s,v,i,p,p,e,t,z,o,n,e
s,h,i,p,p,e,t,z,o,n,e
s,d,i,p,p,e,t,z,o,n,e
s,s,i,p,p,e,t,z,o,n,e
s,e,i,p,p,e,t,z,o,n,e
s,g,i,p,p,e,t,z,o,n,e
s,q,i,p,p,e,t,z,o,n,e
s,m,i,p,p,e,t,z,o,n,e
s,w,i,p,p,e,t,z,o,n,e
s,g,i,p,p,e,t,z,o,n,e
s,x,i,p,p,e,t,z,o,n,e
s,t,i,p,p,e,t,z,o,n,e
s,o,i,p,p,e,t,z,o,n,e
s,e,i,p,p,e,t,z,o,n,e
s,k,i,p,p,e,t,z,o,n,e
s,p,i,p,p,e,t,z,o,n,e
s,b,i,p,p,e,t,z,o,n,e
s,n,i,p,p,e,t,z,o,n,e
found after 176 iterations