JavaScript Golf


(Jack Wearden) #1

At yesterday’s Brum.js we played some JavaScript golf - that is, writing JS that uses as few lines as possible.

We left a challenge up on the website. Feel free to take a shot!

http://golf.butin.space/


(Jack Wearden) #2

Herpderp, I meant characters, not lines. Have fun!


(Jude Gibbons) #3

It won’t let me use it with the address I was using last night… Also, the headline says ‘Repeater’ when the page loads and it’s only when I try to upload a file that it replaces it with the RGB info.


(Jack Wearden) #4

It won’t let me use it with the address I was using last night…

Hmm, that’s odd - I wonder if maybe the profiles expire? I’ll take a look

Also, the headline says ‘Repeater’ when the page loads and it’s only when I try to upload a file that it replaces it with the RGB info.

Ah yeah - that’s because it encourages your browser to cache the page, even though it probably shouldn’t. Will see what I can do, but in the mean time doing cmd+r on mac or ctrl+r on anything else should have it display properly.


(Jack Wearden) #5

I realise I forgot the instructions!

All you need to do is write a function called play. So if upload a js file that contains:

function play(input) { return output }

All you need to do is put your logic inside play, return your output, and the test function will run it :smile:


(Andy Wootton) #6

I used to work with a guy who won a competition for the best C program written in 1 line. It graphically estimated the value of Pi by printing a circular block of asterisks on a line printer. It was completely unreadable, obviously.


(Richard Cunningham) #7

Spent more time on this, than I’d like to admit. 2 less than Jack though :slight_smile:


(Jack Wearden) #8

I closed submissions, you can now see the different implementations :smile:


(Jack Wearden) #9

@rythie Very smart :smiley: You can shave another few chars by ditching the .join() call, as when you implicitly coerce an array to a string it just calls .join() :smile:

My original implementation was

function play(s) {
  r=[]
  i=0
  while (i<5) r.push(+("0x"+s[++i]+s[++i]))
  return "rgb("+r+")"
}

Surprised to see so many different approaches!


(Jude Gibbons) #10

Like @rythie, spent far too long on this… but having seen the results I have now learnt something about bitwise operators! 8-D


(Richard Cunningham) #11

@jackweirdy - Interesting! I never even considered that I could get rid of the join, which should have been more obvious than the parseInt conversion

@Judes you could have saved 6 chars by converting the parseInt line from

 y+=parseInt(n[i]+n[i+1],16)

to

y+="0x"+n[i]+n[i+1]|0

Which is the does an implicit parseInt


(Jude Gibbons) #12

Thanks for that - had another couple of versions (one using ‘join’) but just couldn’t push it below about 96. Have learnt a lot from this, which is what it’s all about! @jackweirdy - any chance you could set a new challenge once a week?! :wink:


(Richard Cunningham) #13

Sam’s entry is quite a neat one and I was able to remove 18 chars from it pretty easily (with what I know now)

function play(s){return'rgb('+s.match(/[^#]{1,2}/g).map(function(c){return parseInt(c,16)}).join()+')'}

becomes

function play(s){return'rgb('+s.match(/\w\w/g).map(function(c){return "0x"+c|0})+')'}

(Jude Gibbons) #14

A couple of these solutions have used |0 after the bit of code that replaces the parseInt - I realise it’s the bitwise OR operator but what is its function here?


(Richard Cunningham) #15

It forces casting from string to int (which seems to happen using parseInt). There are some other ways to do it such as ~~x or +x as in Jack’s one - but I couldn’t use + without adding brackets.

However, I’ve just realised I didn’t even need that in mine - so I could just removed it (since the cast is done in the return line anyway).

This page is pretty good for tips: https://github.com/jed/140bytes/wiki/Byte-saving-techniques


(Jude Gibbons) #16

I did try removing the |0 from your last example and it didn’t work for me (left the 3 colour refs as 4 digit hex codes).


(Richard Cunningham) #17

Sorry, I meant I can remove it from mine, you can’t from yours.


(Jack Wearden) #18

If people want to try it, there’s a new challenge up :smile: Same URL:

http://golf.butin.space/


(Jack Wearden) #19

(you may have to cmd/ctrl+r still to flush the cache - working on that)


(Daniel Hollands) #20

Is there any way to see the old comp? It got replaced before I was able to see the solutions.