-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_asterisksPattern.js
More file actions
84 lines (69 loc) · 2.3 KB
/
app_asterisksPattern.js
File metadata and controls
84 lines (69 loc) · 2.3 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
/*
(Extra) Homework #2 (Optional)
Write a JavaScript program to construct the following pattern, which takes the number of levels as parameter
Function Input: 4
Example output:
*
**
***
****
Make it harder by accepting a 2nd parameter for the position of the asterisks (left, center or right)
*
***
*****
*******
*/
function createAsterisksPattern(levels, position) {
var patternLevels = [];
// Build pattern: starting from y axis 0 and adding characters along x axis
function buildPattern(levels, position) {
for (var y = 0; y < levels; y++) {
// If array element isn't exist then create one to be able to expand later
if (!patternLevels[y]) patternLevels[y] = '';
for (var x = 0; x < levels - y; x++) {
patternLevels[y] = patternLevels[y] + '*';
// In centered position extend the pattern
if (position === 'center' && y !== 0) {
patternLevels[y - 1] = patternLevels[y - 1] + '*';
}
}
}
}
// Adding spaces befor/after elements based on the positioning
function positioningPattern(position) {
for (var y = 1; y < patternLevels.length; y++) {
switch (position) {
case 'left':
for (var x = 0; x < y; x++) {
patternLevels[y] = patternLevels[y] + ' ';
}
break;
case 'right':
for (var x = 0; x < y; x++) {
patternLevels[y] = ' ' + patternLevels[y];
}
break;
case 'center':
for (var x = 0; x < y; x++) {
patternLevels[y] = ' ' + patternLevels[y] + ' ';
}
break;
}
}
}
// Print pattern into the console
function printPattern() {
for (var y = patternLevels.length; y > 0; y--) {
console.log(patternLevels[y - 1]);
}
console.log(patternLevels);
}
buildPattern(levels, position);
positioningPattern(position);
printPattern();
}
// Start application
createAsterisksPattern(4, 'left');
createAsterisksPattern(4, 'right');
createAsterisksPattern(4, 'center');