forked from rocketacademy/basics-drawing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (217 loc) ยท 7.06 KB
/
script.js
File metadata and controls
225 lines (217 loc) ยท 7.06 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// input: number of characters on the screen
// line drawing
var getLine = function (lineLength) {
var drawLine = 0;
var draw = '';
while (drawLine < lineLength) {
draw = draw + '๐';
drawLine = drawLine + 1;
}
return draw;
};
// square drawing
var getSquare = function (squareDimension) {
var draw = '';
var squareHeight = 0;
while (squareHeight < squareDimension) {
var squareLength = 0;
while (squareLength < squareDimension) {
draw = draw + '๐ฅฐ';
squareLength = squareLength + 1;
}
draw = draw + '<br>';
squareHeight = squareHeight + 1;
}
return draw;
};
// triangle drawing
var getTri = function (triDimension) {
var draw = '';
var triHeight = 0;
while (triHeight < triDimension) {
var triRows = 0;
// @height= 0, 1st row doesn't print
// used "<="" to get one extra row
while (triRows <= triHeight) {
draw = draw + '๐ป';
triRows = triRows + 1;
}
draw = draw + '<br>';
triHeight = triHeight + 1;
}
return draw;
};
// reverse triangle drawing
var getRevTri = function (revTriDimension) {
var draw = '';
var revTriHeight = 0;
while (revTriHeight < revTriDimension) {
var revTriRows = 0;
// to reverse triangle, legnth of triangle starts @max input and -1 each loop
var rowLength = revTriDimension - revTriHeight;
while (revTriRows < rowLength) {
draw = draw + '๐';
revTriRows = revTriRows + 1;
}
draw = draw + '<br>';
revTriHeight = revTriHeight + 1;
}
return draw;
};
// Outline Square Drawing
var getOutSquare = function (squareDimension) {
var draw = '';
var squareHeight = 0;
while (squareHeight < squareDimension) {
var squareLength = 0;
// for top and bottom borders
if (squareHeight == 0 || squareHeight == (squareDimension - 1)) {
while (squareLength < squareDimension) {
draw = draw + '๐';
squareLength = squareLength + 1;
}
} else {
while (squareLength < squareDimension) {
// for left and right borders
if (squareLength == 0 || squareLength == (squareDimension - 1)) {
draw = draw + '๐';
squareLength = squareLength + 1;
// fill in everything else
} else {
draw = draw + '๐ฅฐ';
squareLength = squareLength + 1;
}
}
}
draw = draw + '<br>';
squareHeight = squareHeight + 1;
}
// error message to inform user to input higher number to see outline square
if (squareDimension <= 2) {
draw = draw + '<br> Error. In order to see outlined square, please input above 3';
}
return draw;
};
// Center Square Drawing
var getCenSquare = function (squareDimension) {
var draw = '';
var squareHeight = 0;
// +0.5 to get center position
var squareMid = (squareDimension / 2) + 0.5;
// error messages (input validation)
if (squareDimension < 5) {
draw = 'Error. Square dimensions too small, due to limitations please enter only 5, 7, or, 9.';
return draw;
}
if (squareDimension >= 10) {
draw = 'Error. Square dimensions too big, due to limitations please enter only 5, 7, or, 9.';
return draw;
}
if ((squareDimension % 2) == 0) {
draw = 'Error. Please only enter an odd number 5 and above.<br>i.e. 5, 7, 9';
return draw;
}
// valid input -> drawing
if ((squareDimension % 2) == 1) {
while (squareHeight < squareDimension) {
var squareLength = 0;
// prioritize middle square; squareMid - 1 b/c offset 0 count
if (squareHeight == squareMid - 1) {
while (squareLength < squareDimension) {
// for left and right borders
if (squareLength == 0 || squareLength == (squareDimension - 1)) {
draw = draw + '๐';
squareLength = squareLength + 1;
// middle emoji; squareMid - 1 b/c offset 0 count
} else if (squareLength == (squareMid - 1)) {
draw = draw + '๐ฎ';
squareLength = squareLength + 1;
// fill in everything else
} else {
draw = draw + '๐ฅฐ';
squareLength = squareLength + 1;
}
}
// for top and bottom borders
} else if (squareHeight == 0 || squareHeight == (squareDimension - 1)) {
while (squareLength < squareDimension) {
draw = draw + '๐';
squareLength = squareLength + 1;
}
} else {
while (squareLength < squareDimension) {
// for left and right borders
if (squareLength == 0 || squareLength == (squareDimension - 1)) {
draw = draw + '๐';
squareLength = squareLength + 1;
// fill in everything else
} else {
draw = draw + '๐ฅฐ';
squareLength = squareLength + 1;
}
}
}
draw = draw + '<br>';
squareHeight = squareHeight + 1;
}
}
return draw;
};
// "line", "square", "triangle", "reverse-triangle", "outline-square", or "center-square"
var drawMode = 'default';
var main = function (input) {
var myOutputValue = '';
// invalid entry: number < 10
if (input >= 10) {
myOutputValue = 'Error. Sorry, due to limitations, input has to be under 10';
return myOutputValue;
}
// changing modes
if (input == 'line') {
drawMode = 'line-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the number of emojis returned';
return myOutputValue;
}
if (input == 'square') {
drawMode = 'square-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the dimensions of the square';
return myOutputValue;
}
if (input == 'triangle') {
drawMode = 'triangle-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the dimensions of the triangle';
return myOutputValue;
}
if (input == 'reverse-triangle') {
drawMode = 'reverse-triangle-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the dimensions of the reverse triangle';
return myOutputValue;
}
if (input == 'outline-square') {
drawMode = 'outline-square-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the dimensions of the outline square';
return myOutputValue;
}
if (input == 'center-square') {
drawMode = 'center-square-drawing';
myOutputValue = 'Current mode: ' + drawMode + '. Input defines the dimensions of the square';
return myOutputValue;
}
// Drawing!
if (drawMode == 'default') {
myOutputValue = 'Please input a mode: "line", "square", "triangle", "reverse-triangle", "outline-square", or "center-square"';
} else if (drawMode == 'line-drawing') {
myOutputValue = getLine(input);
} else if (drawMode == 'square-drawing') {
myOutputValue = getSquare(input);
} else if (drawMode == 'triangle-drawing') {
myOutputValue = getTri(input);
} else if (drawMode == 'reverse-triangle-drawing') {
myOutputValue = getRevTri(input);
} else if (drawMode == 'outline-square-drawing') {
myOutputValue = getOutSquare(input);
} else if (drawMode == 'center-square-drawing') {
myOutputValue = getCenSquare(input);
}
return myOutputValue;
};