Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Last active August 29, 2015 14:25
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 christiantakle/515f11a5016c24e731fd to your computer and use it in GitHub Desktop.
Save christiantakle/515f11a5016c24e731fd to your computer and use it in GitHub Desktop.
Solution for a codewars task
var
CHAR = '*',
NEWLINE = '\n',
repeat =
function repeat(c, n) {
if(n === 0) { return ''; }
return c + repeat(c, n-1);
},
lines =
function lines(acc, n) {
if(n < 1) { return acc; }
var
core = repeat(CHAR, n),
whitespace = repeat(' ', acc.length+1),
line = [whitespace+core];
return lines(acc.concat(line), n-2);
};
diamond =
function(n) {
if(n <= 0 || n % 2 === 0) { return null; }
var
core = repeat(CHAR, n),
top = lines([], n-2).reverse().join(NEWLINE),
bottom = lines([], n-2).join(NEWLINE);
return [top,core,bottom].join(NEWLINE);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment