multiplication table in javascript

4

This javascript code: for(x=1;x<6;x++)for(y=1;y<6;y++)console.log((y==1?'\n':'')+x+'*'+y+'='+x*y);

prints the following:

1*1=1
1*2=2
1*3=3
1*4=4
1*5=5

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15

4*1=4
4*2=8
4*3=12
4*4=16
4*5=20

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25 

2 questions:

  • Is there a smaller (in bytes) way to do the same?
  • What is the smallest code that can do the same without using 2 for statements?

cherouvim

Posted 2014-09-30T18:42:25.830

Reputation: 149

Question was closed 2014-10-01T17:31:05.643

5

Before the downvotes and close votes come in: this is on-topic

– Martin Ender – 2014-09-30T18:43:26.707

2@MartinBüttner which doesn't change the fact that this is technically a language-specific challenge ;-) – John Dvorak – 2014-09-30T18:47:47.873

1Why are there two blocks of 1s, two blocks of 3s, and no block of 2s or 4s? I don't think that's what the code outputs... – John Dvorak – 2014-09-30T18:49:24.043

1At least in firebug console, the part (y==1?'\n':'') has no effect (no blank lines to separate runs). – edc65 – 2014-09-30T19:06:34.593

@edc65 That is because both Chrome and Firebug print the actual value instead of the string representation as Firefox does. – Optimizer – 2014-09-30T19:14:29.060

@jan Dvorak: you are right. There is a bug with my code. – cherouvim – 2014-09-30T19:44:59.787

I hope you actually require multiplication tables of 1 through 5 instead of what your example output says ... – Optimizer – 2014-09-30T20:08:30.653

@Optimizer: Yes, thanks. It was a bad initial paste. Fixed. – cherouvim – 2014-10-01T06:19:47.210

Answers

4

Single for loop, 80 75 72 68 bytes

I will try to make the code smaller too, but as far as single for loop goes:

for(i=5;i<30;)console.log(k=i/5|0,"*",l=i%5+1,"=",k*l,++i%5?"":"\n")

Try it in your console! (Although IE gives the best results)

Optimizer

Posted 2014-09-30T18:42:25.830

Reputation: 25 836

Don't forget to include the code length in the header – John Dvorak – 2014-09-30T18:49:53.683

"Is there a smaller (in bytes) way to do the same?" -- so, it surely is code-golf – John Dvorak – 2014-09-30T18:51:00.453

Should I add it? – John Dvorak – 2014-09-30T18:52:41.943

There. It's tagged code-golf now. – John Dvorak – 2014-09-30T18:53:37.470

Try in your MSIE console! In chrome and firefox and firebug the output is very different... (no problem for me, just to make it clear) – edc65 – 2014-09-30T19:31:51.833

Yeah, There is a discussion going wrt Fx Devtools to remove the quotes, then it will be equivalent to MSIE. Chrome and Firebug are both buggy though ... – Optimizer – 2014-09-30T19:33:39.047

Assigning and printing the result (k=i/5|0) is a neat trick. Thanks. – cherouvim – 2014-10-01T06:24:12.293

@cherouvim - Yeah, there are many other tricks that you can find at http://codegolf.stackexchange.com/questions/37624/tips-for-golfing-in-ecmascript-6/ and http://codegolf.stackexchange.com/questions/2682/tips-for-golfing-in-javascript

– Optimizer – 2014-10-01T06:51:57.167