-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge-04.js
44 lines (38 loc) · 967 Bytes
/
challenge-04.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
function createXmasTree(height) {
var lines = [];
var width = height + height - 1;
var line;
var stars = 1;
for (var i=0; i<height; i++) {
var mid = height-parseInt(stars/2)-1;
line = '';
var stars_painted = 0;
for (var j=0; j<width; j++) {
if (j>=mid && stars_painted<stars) {
line += '*';
stars_painted += 1;
} else {
line += '_';
}
}
lines.push(line);
stars += 2;
}
for (i=0; i<2; i++) {
line = '';
for (j=0; j<width; j++) {
var mid = parseInt(width/2);
if (j===mid) {
line += '#';
} else {
line += '_';
}
}
lines.push(line);
}
var tree = lines.join('\n');
return tree;
}
console.log(createXmasTree(5));
console.log(createXmasTree(3));