Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Created September 8, 2015 22:02
Show Gist options
  • Save jluismm2311/ddb3c6ed0c2109e0e8f5 to your computer and use it in GitHub Desktop.
Save jluismm2311/ddb3c6ed0c2109e0e8f5 to your computer and use it in GitHub Desktop.
Create a function christmasTree(height) that returns a christmas tree of the correct height christmasTree(5) should return: * *** ***** ******* ********* Height passed is always an integer between 0 and 100. Use \n for newlines between each line. Pad with spaces so each line is the same length. The last line having only stars, no spaces.
function christmasTree(height) {
var value = "";
for(i = 1; i<= height; i++ ) {
var blank = " ".repeat(height-i);
value += blank + "*".repeat(1+((i-1)*2)) + blank;
value += i < height ? "\n" : "";
}
return value
}
String.prototype.repeat = function(times) {
return (new Array(times + 1)).join(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment