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
97 changes: 92 additions & 5 deletions assets/js/shapes.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,118 @@
function getLine(length) {
// TODO - write method definition here

var line = "";
for(var count = 0; count < length; count++){
line += "*";
}
return line;
}



function getBox(width, height) {
// TODO - write method definition here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not bad! Not a huge fan of the implementation though.
Consider code-reuse.

It is less redundant to reuse code that has already been written to perform what you need.
It could be more simply expressed as

function getBox(width, height) {
    let output="";
    for(let i=0;i<height;i++) {
        output += getLine(width) + "\n";
    }
    return output;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely know i have this issue when coding. I have been getting better at seeing when I start using redundant coding(it is easy to catch when I start copying and pasting lol). I was not sure how to make a new function in JS to try and stick the redundant code in.

var hCount = 0;
var wCount = 0;
var box = "";
while(hCount < height){
wCount = 0;
while(wCount < width){
box += "*"
wCount++;
}
box += "\n";
wCount = 0;
hCount++;
}
return box;
}



function getBottomLeftTriangle(length) {
// TODO - write method definition here
var count = 0;
var numOfStarsForLine = 1;
var bottomLeftTriangle = "";
while(count < length){
for(var starCount = 0; starCount < numOfStarsForLine; starCount++){
bottomLeftTriangle += "*";
}

bottomLeftTriangle += "\n";
count++;
numOfStarsForLine++;
}
return bottomLeftTriangle;
}


function getUpperLeftTriangle(length) {
// TODO - write method definition here
var count = 0;
var numOfStarsForLine = length;
var upperLeftTriangle = "";
while(count < length){
for(var starCount = 0; starCount < numOfStarsForLine; starCount++){
upperLeftTriangle += "*";
}

upperLeftTriangle += "\n";
count++;
numOfStarsForLine--;
}
return upperLeftTriangle;
}



function getPyramid(length) {
// TODO - write method definition here
function getPyramid(length){
var count = 0;
var starsPerLine = 1;
var pyramid = "";
while(count < length){
if(length - count == 3){
pyramid += " ";
}
else if(length - count == 2){
pyramid += " ";
}
for(var starCount = 0; starCount < starsPerLine; starCount++){
pyramid += "*";
}
pyramid += "\n";
starsPerLine += 2;
count++;
}
return pyramid;
}


function getCheckerboard(width, height) {
// TODO - write method definition here
var hCount = 0;
var wCount = 0;
var checkerBoard = "";

for(var hCount = 0; hCount < height; hCount++){
for(wCount = 0; wCount < width; wCount++){
if(hCount % 2 == 0){
if(wCount % 2 == 0){
checkerBoard += " ";
}
else{
checkerBoard += "*";
}
}
else{
if(wCount % 2 == 0){
checkerBoard += "*";
}
else{
checkerBoard += " ";
}
}
}
checkerBoard += "\n";
}
return checkerBoard;
}