Last active
August 29, 2015 14:19
-
-
Save SyntaxStacks/051c298ea8f86c61af87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
minified: | |
var n=9;for(var i=1;i<n*2;i++){for(var s=n-(Math.abs(i-n)),k=s-1;k>0;k--){s=[k,s,k].join("");}console.log(new Array(Math.abs(i-n)+2).join(" ")+s);} | |
146 characters | |
Clean: | |
var num = 9; | |
for(var i = 1; i < num*2; i++ ) { | |
var seq = num-(Math.abs(i-num)); | |
for(var k = seq-1; k > 0; k--) { | |
seq = [k,seq,k].join(''); | |
} | |
var space = new Array(Math.abs(i-num)+1).join(" "); | |
console.log(space+seq+"\n"); | |
} | |
/* | |
1 | |
121 | |
12321 | |
1234321 | |
123454321 | |
12345654321 | |
1234567654321 | |
123456787654321 | |
12345678987654321 | |
123456787654321 | |
1234567654321 | |
12345654321 | |
123454321 | |
1234321 | |
12321 | |
121 | |
1 | |
*/ | |
----------------------------------------------------------------------- | |
minified: | |
var n=9,s=n;for(var k=n-1;k>0;k--){s=[k,s,k].join('');}var out=[s];for(var i=n;i>1;i--){s=s.split(''+i+(i-1)).join('');out.unshift([new Array(Math.floor((out[Math.floor(out.length/2)].length-s.length))/2+1).join(' ')+s]);out.push(out[0]);}console.log(out.join('\n')); | |
var n=9,s=n; | |
for(var k=n-1;k>0;k--){ | |
s=[k,s,k].join(''); | |
} | |
var out=[s]; | |
for(var i=n;i>1;i--){ | |
s=s.split(''+i+(i-1)).join(''); | |
out.unshift([new Array(Math.floor((out[Math.floor(out.length/2)].length-s.length))/2+1).join(' ')+s]); | |
out.push(out[0]); | |
} | |
console.log(out.join('\n')); | |
fewer loop iterations and one output statement, slightly longer code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment