-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path010Sierpinski.js
More file actions
50 lines (43 loc) · 1.11 KB
/
Copy path010Sierpinski.js
File metadata and controls
50 lines (43 loc) · 1.11 KB
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
45
46
47
48
49
50
/* Write a function that takes an integer n and returns the nth iteration of the
fractal Sierpinski's gasket. The fractal is composed entirely of ‘L’ and white-space characters; each character has one space between it and the next (or a newline). CodeWars 6th kyu challenge */
/* Example iterations given in problem statement; I replaced space characters
with bullets for easier viewing/counting.
0: L
1: L
L•L
2: L
L•L
L•••L
L•L•L•L
3: L
L•L
L•••L
L•L•L•L
L•••••••L
L•L•••••L•L
L•••L•••L•••L
L•L•L•L•L•L•L•L
*/
function sierpinski(n) {
var lines = addLines(n);
return lines.join('\n');
function addLines(n) {
if (n === 0) {
return ['L'];
} else {
var prev = addLines(n - 1);
var limit = Math.pow(2, (n - 1));
for (var i = 0; i < limit; i++) {
var howManySpaces = Math.pow(2, n) - (2*i + 1);
var spaces = '';
while (howManySpaces > 0) {
spaces += ' ';
howManySpaces--;
}
var line = prev[i] + spaces + prev[i];
prev.push(line);
}
return prev;
}
}
}