Okay, I recently wrote a Javascript Rubik's Cube scrambler that outputs a 25-move scramble. It's 135 bytes. The Gist is here, but for reference, I have the formatted code:

  return s.join(' ')

Any tips?

EDIT: Code is updated, with @Optimizer's edits.

  • \$\begingroup\$ I am assuming that you need to run this in browser, so ES5 and below only ? \$\endgroup\$
    – Optimizer
    May 29, 2015 at 22:34
  • \$\begingroup\$ Some of the answers are likely to be complete rewrites rather than tips for lowering the byte count of your algorithm/implementation. \$\endgroup\$
    – Sparr
    May 29, 2015 at 22:35
  • \$\begingroup\$ @Optimizer Yeah, you have to run in a browser console. \$\endgroup\$ May 29, 2015 at 22:40

1 Answer 1


140 135 127 bytes

Still a big one, but here's for starters:

function(){for(a=s=y=r=[],x=Math.random;a<25;s[a++]=r+["'",2,''][0|x(y=r)*3])for(;r==y;r='RLUDFB'[0|x()*6]);return s.join(' ')}

Some of the few tips used:

  • Loose the var. There is no point in using var when your aim is to golf!
  • When a = [], a++ gives 0. We use this fact to remove a 0 and ,
  • Combine the a<25 and a++
  • You don't need to split a string. You can access its characters anyways via [i]
  • Get rid of {} in for loops

More to come.

  • \$\begingroup\$ Just wondering, why are the parentheses after the function not there? \$\endgroup\$ May 29, 2015 at 23:11
  • \$\begingroup\$ @molarmanful Whoops \$\endgroup\$
    – Optimizer
    May 29, 2015 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.