-
-
Save Kubo2/c18932d688326611d7e4f92827103fd5 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Star diamond | |
*/ | |
String.prototype.repeat = function(n) { | |
if(n < 0) throw new RangeError('Invalid count value'); | |
var ret = ''; | |
while(n--) ret += this; | |
return ret; | |
}; | |
function diamond(n) { | |
if(n < 1 || n % 2 == 0) return null; | |
var diamond = ''; | |
for( | |
var | |
line = (n - 1) / 2, | |
boundary = -line - 1, | |
indent; | |
line > boundary; | |
line-- | |
) { | |
indent = Math.abs(line); | |
diamond += ' '.repeat(indent) + '*'.repeat(n - 2 * indent) + '\n'; | |
} | |
return diamond; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment