Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions assets/js/shapes.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,93 @@
function getLine(length) {
// TODO - write method definition here
let shape = "";
for(let i = 1; i <= length; i++) shape += "*";
return shape;
}



function getBox(width, height) {
// TODO - write method definition here
let shape = "";
for(let i = 1; i <= height; i++) {

// Creating a new line
shape += getLine(width) + "\n";
}
return shape;
}



function getBottomLeftTriangle(length) {
// TODO - write method definition here
let shape = "";
for(let i = 1; i <= length; i++) {
shape += getLine(i);
if(i != length) shape += "\n";
}
return shape;
}


function getUpperLeftTriangle(length) {
// TODO - write method definition here
let shape = "";
for(let i = length; i >= 1; i--) {
shape += getLine(i);
if(i != 1) shape += "\n";
}
return shape;
}



function getPyramid(length) {
// TODO - write method definition here
let shape = "";

if(length == 0 || length == 1) return getLine(length);

for(let i = 1; i <= length; i++) {
let line = "";

// Adding leading space
for(let j = 1; j <= length - i; j++) {
line += " ";
}

// Adding stars
if(i == 1) {
line += "*";
} else {
for(let j = 1; j <= 1 + Math.pow(2, i - 1); j++) {
line += "*";
}
}

// Adding extra space after the stars
for(let j = 1; j <= length - i; j++) {
line += " ";
}

shape += line;
if(i != length) shape += "\n";
}
return shape;
}


function getCheckerboard(width, height) {
// TODO - write method definition here
let shape = "";

for(let i = 1; i <= height; i++) {
let line = "";
for(let j = 1; j <= width; j++) {

// Adding space or star
if((i % 2 == 0 && j % 2 == 0) || (i % 2 == 1 && j % 2 == 1)) {
line += " ";
} else {
line += "*";
}
}
shape += line + "\n";
}
return shape;
}