Skip to content

Instantly share code, notes, and snippets.

@dennis90
Created April 1, 2023 16:00
Show Gist options
  • Save dennis90/bd4f778ec9c58cb5b050a4c561437bfa to your computer and use it in GitHub Desktop.
Save dennis90/bd4f778ec9c58cb5b050a4c561437bfa to your computer and use it in GitHub Desktop.
exercicio
const SQUARE_WIDTH = 9;
const HEIGHT = 9;
const SPACING = 5;
const DRAW_CHAR = "*";
function drawSquare(line) {
let square = "";
for (let row = 0; row < SQUARE_WIDTH; row++) {
if (line === 0 || line === HEIGHT - 1) {
square += DRAW_CHAR;
} else {
if (row === 0 || row === SQUARE_WIDTH - 1) {
square += DRAW_CHAR;
} else {
square += " ";
}
}
}
return square;
}
function drawOval(line) {
const CIRCLE_WIDTH = 10;
let oval = "";
const spaces =
line === 0 || line === HEIGHT - 1
? 3
: line === 1 || line === HEIGHT - 2
? 1
: 0;
for (let row = 0; row < CIRCLE_WIDTH; row++) {
if (
row === spaces ||
row === CIRCLE_WIDTH - spaces - 1 ||
((line === 0 || line === HEIGHT - 1) &&
row > spaces &&
row < CIRCLE_WIDTH - spaces - 1)
) {
oval += DRAW_CHAR;
} else {
oval += " ";
}
}
return oval;
}
function drawArrow(line) {
const ARROW_WIDTH = 5;
const spaces = line < 3 ? 2 - line : 2;
let arrow = "";
for (let row = 0; row < ARROW_WIDTH; row++) {
if (row < spaces || row >= ARROW_WIDTH - spaces) {
arrow += " ";
} else {
arrow += DRAW_CHAR;
}
}
return arrow;
}
function drawDiamond(line) {
let diamond = "";
const half = Math.floor(SQUARE_WIDTH / 2);
const spaces = line < half ? half - line : half - (SQUARE_WIDTH - line - 1);
for (let row = 0; row < SQUARE_WIDTH; row++) {
if (row < spaces || row >= SQUARE_WIDTH - spaces) {
diamond += " ";
} else {
if (row === spaces || row === SQUARE_WIDTH - spaces - 1) {
diamond += DRAW_CHAR;
} else {
diamond += " ";
}
}
}
return diamond;
}
function printBoard() {
for (let i = 0; i < HEIGHT; i++) {
let row = "";
row += drawSquare(i);
row += " ".repeat(SPACING);
row += drawOval(i);
row += " ".repeat(SPACING);
row += drawArrow(i);
row += " ".repeat(SPACING);
row += drawDiamond(i);
console.log(row);
}
}
printBoard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment