Skip to content

Instantly share code, notes, and snippets.

@Zhangerr
Created October 4, 2014 19:46
Show Gist options
  • Save Zhangerr/c4c4693f5a93231f7fd7 to your computer and use it in GitHub Desktop.
Save Zhangerr/c4c4693f5a93231f7fd7 to your computer and use it in GitHub Desktop.
Diamond generator
// *
// ***
// *****
// *******
// *********
// *******
// *****
// ***
// *
// *
// ***
// *****
// ***
// *
function diamond(n) {
for(var i = 0; i < n; i++) {
var spaces = Math.abs((n-1)/2-i)
var ast = i <= (n-1)/2? 1+2*i : n-2*(i-(n-1)/2)
var s = ''
for(var c = 0; c < spaces; c++) {
s += ' '
}
for(var c = 0; c < ast; c++) {
s += '*'
}
console.log(s)
}
}
for(var i = 1; i< 59; i+=2) {
setTimeout(diamond,20*i,i)
}
for(var i = 59; i>0; i-=2) {
setTimeout(diamond,50*20+20*(59-i),i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment