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?
5
Before the downvotes and close votes come in: this is on-topic
– Martin Ender – 2014-09-30T18:43:26.7072@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
1
s, two blocks of3
s, and no block of2
s or4
s? I don't think that's what the code outputs... – John Dvorak – 2014-09-30T18:49:24.0431At 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