Really short Javascript cube timer

cubernya

Using an interval for a timer is not a good idea, as it gets inaccurate pretty quickly. Use the system's time instead (I'm sure this can be quite small as well)

molarmanful

Member
Using an interval for a timer is not a good idea, as it gets inaccurate pretty quickly. Use the system's time instead (I'm sure this can be quite small as well)
I considered that approach; however, I decided to stick with intervals because they saved more bytes. Such is code golf...

EDIT: Okay, I decided to go with your approach. At least it's got accuracy.

Last edited:

00

Member
if you hold one key down and then press another, everything breaks.

molarmanful

Member
if you hold one key down and then press another, everything breaks.
Fixed. Also got byte count down to 316 bytes.

molarmanful

Member
Bump...

Got down to 261 bytes:
Code:
function z(){for(c=b=j=25,r=Math.random,s[i='innerHTML']='';j;c+b-5|c-m&&b-m?s[i]+="URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" ":0)m=0|r()*6}z(),k=0,onkeyup=function(x){x.which^32||k?(clearInterval(T),z(),k=0):(S=new Date,k=1,T=setInterval("a[i]=(new Date-S)/1e3"))}
- Change all document.getElementById stuff to shorter variants.
- Use onkeyup.
- Change == to ^ and && to ||.
- Take out second setInterval parameter.

Any more suggestions for lowering byte count?

Last edited:

molarmanful

Member
More saves (258):
Code:
function z(){for(c=b=j=25,r=Math.random,s[i='innerHTML']='';j;c+b-5|c-m&&b-m?s[i]+="URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" ":0)m=0|r()*6}z(),k=0,onkeyup=function(x){x.which^32||k?(clearInterval(T),z()):(S=new Date,T=setInterval("a[i]=(new Date-S)/1e3"));k=!k}
-Just merged k=0 and k=1 into k=!k.

molarmanful

Member
256 now:
Code:
function z(){for(c=b=j=25,r=Math.random,s[i='innerHTML']='';j;c+b-5|c-m&&b-m?s[i]+="URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" ":0)m=0|r()*6}z(),k=onkeyup=function(x){x.which^32||k?(S=new Date,T=setInterval("a[i]=(new Date-S)/1e3")):(clearInterval(T),z());k=!k}
-Declare k with onkeyup.