12 Replies - 1750 Views - Last Post: 07 May 2014 - 03:42 AM

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

JS code golf

Posted 09 February 2011 - 07:08 PM

Par 2

Well, I have noticed the other forums doing this, and thought that we would add a challenge of the same type here.

For hole one you just want to get as short a code segment as you can (you can use any language though you get bonus points if you use JS).

Here is what the script must accomplish:

Output five rows of numbers counting up from 1. On the first row there should be 1 number, the second should have 2, and so on down the line until you have finished. Each number should be comma separated. The last number on a line should not have a trailing comma.

Example output:
1
2, 3
4, 5, 6
7, 8, 9, 10


What to count:
Count all non-whitespace characters. Then count all required whitespace characters (spaces).

For example for(var i = 0; i < 10; i++); is only 22 characters because the space between var and i is required, but the rest of the spaces can be removed while not changing the program's output.

Have fun and go for the lowest character count you can get.

Is This A Good Question/Topic? 4
  • +

Replies To: JS code golf

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: JS code golf

Posted 09 February 2011 - 08:55 PM

t="";
n=1;
for(i=1;i<6;i++){
  for(j=0;j<i;j++){
    t+=(j==0)?n:", "+n;
    n++
  }
  t+="\n"
}
alert(t)


I got 82 characters. You didn't specify how the result was to be outputted, so I just alerted it. Also, a newline can count as required whitespace in the absence of semicolons.

This post has been edited by forest51690: 09 February 2011 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: JS code golf

Posted 10 February 2011 - 09:27 AM

Ah, yes. I suppose you are correct. The newline character counts if, in the case you don't have it, the code will not run.

I didn't specify the output method because you can use any output method you want. You could do document.write, document.body.innerHTML, or whatever. It also doesn't require that the language is JS (because you could output to terminal, etc.).

I managed to get it to 94 characters using only 1 for loop.

Spoiler


Edit - managed to get it down to 89 characters:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: JS code golf

Posted 13 February 2011 - 07:16 PM

That's some clever code!
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: JS code golf

Posted 14 February 2011 - 03:57 PM

Thanks, I managed to reduce it by 5 characters as well. It is now a whopping 89 characters, but it still just uses 1 loop.

Too bad more people don't seem to be interested in these, I have some extremely interesting (well, at least I think so) 'holes' thought up, but I would like this to get a bit more popular before we go there.

<edit>
Speaking of which, I have added the second 'hole' a few minutes ago.
</edit>
Was This Post Helpful? 0
  • +
  • -

#6 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: JS code golf

Posted 14 February 2011 - 08:24 PM

I'm hooked. But the cynic in me can't but help to point out that you're both over-thinking the prompt... 52 characters ;)
alert("1\n2, 3\n4, 5, 6\n7, 8, 9, 10\n11, 12, 13, 14, 15");


Now let me see if I can come up with anything more clever and less snarky :P
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: JS code golf

Posted 15 February 2011 - 12:04 AM

Lol, yeah. While that does give the output, it also isn't the goal of the challenge :)
Was This Post Helpful? 0
  • +
  • -

#8 WallyRWest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 13

Re: JS code golf

Posted 04 December 2013 - 11:34 PM

View PostBetaWar, on 10 February 2011 - 09:27 AM, said:

Edit - managed to get it down to 89 characters:
Spoiler


Nice, I'll see your 88 (removing all line breaks) characters...

...and raise you 83...

c=i=1,j=0,s="";for(;i<6;!(++j%i)&&(i++,j=0,s+="\n"))s+=(c++)+(j<i-1&&", ");alert(s)


Explanation:
1. no variable t (-2)
2. ternary :0 to indicate false, convert x?y:0 to x&&y (-1 x 2)
3. j!=i-1 when we know j will never get equal or beyond, convert to j<i-1 (-1)

~Wally
Was This Post Helpful? 0
  • +
  • -

#9 WallyRWest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 13

Re: JS code golf

Posted 04 December 2013 - 11:50 PM

View PostWallyRWest, on 04 December 2013 - 11:34 PM, said:

View PostBetaWar, on 10 February 2011 - 09:27 AM, said:

Edit - managed to get it down to 89 characters:
Spoiler


Nice, I'll see your 88 (removing all line breaks) characters...

...and raise you 83...

c=i=1,j=0,s="";for(;i<6;!(++j%i)&&(i++,j=0,s+="\n"))s+=(c++)+(j<i-1&&", ");alert(s)


Explanation:
1. no variable t (-2)
2. ternary :0 to indicate false, convert x?y:0 to x&&y (-1 x 2)
3. j!=i-1 when we know j will never get equal or beyond, convert to j<i-1 (-1)

~Wally


Actually... now that I think of it, I can reduce it further... to 81...
c=i=1;j=0;for(s="";6>i;!(++j%i)&&(i++,j=0,s+="\n"))s+=c++ +(j<i-1&&", ");alert(s)


Explanation:
1. s initially starts off as a blank variable, could easily be thrown into the loop init... one less comma (-1)
2. s+=(c++)+ is a bit lengthy... remove brackets, insert necessary space:
So "s+=(c++)+" becomes "s+=c++ +" (-1)
Was This Post Helpful? 0
  • +
  • -

#10 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,840
  • Joined: 30-April 10

Re: JS code golf

Posted 05 December 2013 - 08:30 AM

From 2011 to the end of 2013. Neat.
Was This Post Helpful? 0
  • +
  • -

#11 s__  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-February 14

Re: JS code golf

Posted 13 February 2014 - 11:16 PM

Spent way too long on this.

a. 74
for (o = c = i = 1; i < 5; o += "\n" + a)
	for (j = i++, a = [++c]; j; --j)
		a[i - j] = " " + ++c
alert(o)

b. 71 (forest51690's approach)
t = "";
for (i = n = 1; i < 6; i++, t += "\n")
	for (j = 0; j < i; n++)
		t += j++ ? ", " + n : n
alert(t)

c. 68
for (i = o = 1; i < 15; o += ++i)
	o += (Math.sqrt(8 * i + 1) + 1) / 2 % 1 ? ", " : "\n"
alert(o)

d. 67 (like c, but doesn't scale well)
for (i = o = 1; i < 15; o += ++i)
	o += i == 1 || i == 3 || i == 6 || i == 10 ? "\n" : ", "
alert(o)

e. 66 (BetaWar's approach)
s = ""
for (j = i = c = 1; i < 6;)
	s += c++ +(++j % i ? ", " : (i++, j = 0, "\n"))
alert(s)

f. 63 (as d but more so)
for (i = o = 1; i < 15; o += ++i)
	o += [, 1,, 1,,, 1,,,, 1][i] ? "\n" : ", "
alert(o)

Only 5 chars more than KuroTsuto's entry! Can it be beaten?
Was This Post Helpful? 0
  • +
  • -

#12 immaterial  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-May 14

Re: JS code golf

Posted 07 May 2014 - 02:30 AM

Hiya

I've just got it down to 58 :-)
for(n=c=m=a=1;n++<15;a+=n)a+=--c?",":(c=++m,"\n");alert(a)


Although if i'm allowed to execute in a browser & output to the console, it doesn't need the alert(a) & can just return the last value used (works for me in Chrome,Firefox,Opera,IE)
for 49 characters :
for(n=c=m=a=1;n++<16;a+=n)a+=--c?",":(c=++m,"\n")


-imma
Was This Post Helpful? 0
  • +
  • -

#13 immaterial  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-May 14

Re: JS code golf

Posted 07 May 2014 - 03:42 AM

from
for(n=c=m=a=1;n++<16;a+=n)a+=--c?",":(c=++m,"\n")

move the +n to the loop statement x2 (and lower the max by 1) : still 49c
for(n=c=m=a=1;n++<15;)a+=--c?","+n:(c=++m,"\n"+n)

replace the ","+n with [,n] for one more character : 48c
for(n=c=m=a=1;n++<15;)a+=--c?[,n]:(c=++m,"\n"+n)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1