Skip to content

Instantly share code, notes, and snippets.

@Kubo2

Kubo2/diamond.js Secret

Last active April 24, 2016 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kubo2/c18932d688326611d7e4f92827103fd5 to your computer and use it in GitHub Desktop.
Save Kubo2/c18932d688326611d7e4f92827103fd5 to your computer and use it in GitHub Desktop.
/**
* 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