Last active
August 29, 2015 14:25
-
-
Save christiantakle/515f11a5016c24e731fd to your computer and use it in GitHub Desktop.
Solution for a codewars task
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
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