Skip to content

Commit fb7a035

Browse files
adding more exercise
drawing figures with loops
1 parent 895f6a2 commit fb7a035

File tree

10 files changed

+199
-0
lines changed

10 files changed

+199
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function rectangleOf10x10Stars() {
2+
let width = 10;
3+
let height = 10;
4+
5+
for (let i = 0; i < height; i++) {
6+
let row = "";
7+
for (let j = 0; j < width; j++) {
8+
row += "*";
9+
}
10+
console.log(row);
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function rectangleOfNxNStars(input) {
2+
let n = parseInt(input[0]);
3+
4+
for (let i = 0; i < n; i++) {
5+
let row = "";
6+
for (let j = 0; j < n; j++) {
7+
row += "*";
8+
}
9+
console.log(row);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function squareOfStars(input) {
2+
let n = parseInt(input[0]);
3+
4+
for (let i = 0; i < n; i++) {
5+
let row = "";
6+
for (let j = 0; j < n; j++) {
7+
row += "* ";
8+
}
9+
console.log(row.trim());
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function triangleOfDollars(input) {
2+
let n = parseInt(input[0]);
3+
4+
for (let row = 1; row <= n; row++) {
5+
let output = "";
6+
for (let col = 1; col <= row; col++) {
7+
output += "$ ";
8+
}
9+
console.log(output.trim());
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function squareFrame(input) {
2+
let n = parseInt(input[0]);
3+
4+
for (let i = 0; i < n; i++) {
5+
if (i == 0 || i == n - 1) {
6+
let row = "+";
7+
for (let j = 0; j < n - 2; j++) {
8+
row += " -";
9+
}
10+
console.log(row + " +");
11+
} else {
12+
let row = "|";
13+
for (let j = 0; j < n - 2; j++) {
14+
row += " -";
15+
}
16+
console.log(row + " |");
17+
}
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function rhombusOfStars(input) {
2+
let n = parseInt(input[0]);
3+
4+
for (let row = 1; row <= n; row++) {
5+
let output = "";
6+
for (let spaces = 1; spaces <= n - row; spaces++) {
7+
output += " ";
8+
}
9+
output += "*";
10+
for (let stars = 1; stars < row; stars++) {
11+
output += " *";
12+
}
13+
console.log(output);
14+
}
15+
16+
for (let row = n - 1; row >= 1; row--) {
17+
let output = "";
18+
for (let spaces = 1; spaces <= n - row; spaces++) {
19+
output += " ";
20+
}
21+
output += "*";
22+
for (let stars = 1; stars < row; stars++) {
23+
output += " *";
24+
}
25+
console.log(output);
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function christmasTree(input) {
2+
let n = parseInt(input[0]);
3+
4+
function repeatString(toRepeat, count) {
5+
let result = "";
6+
for (let i = 0; i < count; i++) {
7+
result += toRepeat;
8+
}
9+
return result;
10+
}
11+
12+
for (let i = 0; i <= n; i++) {
13+
let spaces = repeatString(" ", n - i);
14+
let stars = repeatString("*", i);
15+
console.log(spaces + stars + " | " + stars + spaces);
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function sunglasses(input) {
2+
const n = parseInt(input[0]);
3+
4+
function repeatSymbol(symbol, count) {
5+
const result = new Array(count).fill(symbol).join('');
6+
return result;
7+
}
8+
9+
const stars = repeatSymbol('*', n * 2);
10+
const slashes = repeatSymbol('/', n * 2 - 2);
11+
const spaces = repeatSymbol(' ', n);
12+
13+
console.log(stars + spaces + stars);
14+
15+
for (let i = 0; i < n - 2; i++) {
16+
if (i === Math.floor((n-1) / 2) - 1) {
17+
console.log('*' + slashes + '*' + repeatSymbol('|', n) + '*' + slashes + '*');
18+
} else {
19+
console.log('*' + slashes + '*' + spaces + '*' + slashes + '*');
20+
}
21+
}
22+
23+
console.log(stars + spaces + stars);
24+
}
25+
26+
sunglasses(['6']);
27+
// sunglasses(['4']);
28+
// sunglasses(['5']);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function house(input) {
2+
const n = parseInt(input[0]);
3+
4+
function repeatString(toRepeat, count) {
5+
let result = "";
6+
for (let i = 0; i < count; i++) {
7+
result += toRepeat;
8+
}
9+
return result;
10+
}
11+
12+
let starsCount = (n % 2 === 0) ? 2 : 1;
13+
14+
for (let i = 0; i < Math.ceil(n / 2); i++) {
15+
const stars = repeatString("*", starsCount);
16+
const dashes = repeatString("-", (n - starsCount) / 2);
17+
console.log(dashes + stars + dashes);
18+
starsCount += 2;
19+
}
20+
21+
const baseRows = repeatString("|" + repeatString("*", n - 2) + "|\n", Math.floor(n / 2));
22+
console.log(baseRows);
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function diamond(input) {
2+
const n = parseInt(input[0]);
3+
let leftRight = Math.floor((n - 1) / 2);
4+
5+
for (let i = 0; i < Math.ceil(n / 2); i++) {
6+
let result = '';
7+
result += '-'.repeat(leftRight);
8+
result += '*';
9+
const mid = n - 2 * leftRight - 2;
10+
11+
if (mid >= 0) {
12+
result += '-'.repeat(mid);
13+
result += '*';
14+
}
15+
16+
result += '-'.repeat(leftRight);
17+
18+
console.log(result);
19+
leftRight--;
20+
}
21+
22+
leftRight = 1;
23+
for (let i = Math.floor((n - 1) / 2 - 1); i >= 0; i--) {
24+
let result = '';
25+
result += '-'.repeat(leftRight);
26+
result += '*';
27+
28+
const mid = n - 2 * leftRight - 2;
29+
30+
if (mid >= 0) {
31+
result += '-'.repeat(mid);
32+
result += '*';
33+
}
34+
35+
result += '-'.repeat(leftRight);
36+
37+
console.log(result);
38+
leftRight++;
39+
}
40+
}

0 commit comments

Comments
 (0)