Skip to content

Instantly share code, notes, and snippets.

@Kubo2 Kubo2/diamond.js Secret
Last active Apr 24, 2016

Embed
What would you like to do?
/**
* 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
You can’t perform that action at this time.