From 56c16fea1a4ef38431f80015b9224d0c9bf47445 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:07:34 -0400 Subject: [PATCH 1/8] Finished assignment --- assets/js/footer-functions.js | 2 +- assets/js/shapes.js | 109 +++++++++++++++++++++++++++++++--- index.html | 1 - 3 files changed, 103 insertions(+), 9 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 42cc05b..a6ff491 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -24,7 +24,7 @@ function getBoxTests() { function getBottomLeftTriangleTests() { testGetBottomLeftTriangle("*", 1); testGetBottomLeftTriangle("*\n**", 2); - testGetBottomLeftTriangle("*\n**\n**", 3); + testGetBottomLeftTriangle("*\n**\n***", 3); } function getUpperLeftTriangleTests() { diff --git a/assets/js/shapes.js b/assets/js/shapes.js index e4351ba..639e584 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -1,31 +1,126 @@ function getLine(length) { - // TODO - write method definition here -} + if (length < 0) { + throw new Error("Illegal arguement"); + } + var line = ""; + while (length > 0) { + line += "*"; + length--; + } + return line; +} function getBox(width, height) { - // TODO - write method definition here + if (width < 0) { + throw new Error("width needs to be greater than 0"); + } else if (height < 0) { + throw new Error("height needs to be greater than 0"); + } + var line = new String(); + for (var heightIndex = 0; height > heightIndex; heightIndex++) { + for (var widthIndex = 0; width > widthIndex; widthIndex++) { + line += "*"; + } + line += "\n"; + } + return line; } function getBottomLeftTriangle(length) { - // TODO - write method definition here + if (length < 0) { + //throw new Error("length needs to be greater than 0"); + return ""; + } + var triangle = new String(); + var lineNumber = 0; + while (length > lineNumber) { + var amount = 0; + while (lineNumber >= amount) { + triangle += "*"; + amount++; + } + lineNumber++; + if (lineNumber != length) { + triangle += "\n"; + } + } + return triangle; } function getUpperLeftTriangle(length) { - // TODO - write method definition here + if (length < 0) { + //throw new Error("length needs to be greater than 0"); + return ""; + } + var triangle = new String(); + var lineNumber = length; + while (lineNumber > 0) { + var amount = 0; + while (lineNumber > amount) { + triangle += "*"; + amount++; + } + lineNumber--; + if (lineNumber != 0) { + triangle += "\n"; + } + } + return triangle; } function getPyramid(length) { - // TODO - write method definition here + if (length < 0) { + throw new Error("length needs to be greater than 0"); + } + col = length * 2 - 1; + row = length; + var amountStars = 1; + var pyramid = new String(); + for (var j = 0; row > j; j++) { + var amountSpaces = Math.floor((col - amountStars) / 2); + console.log("Math.floor((col - amountStars) / 2)" + Math.floor((col - amountStars) / 2)); + for (var i = 0; amountSpaces > i; i++) { + pyramid += " "; + } + for (var i = 0; amountStars > i; i++) { + pyramid += "*"; + } + for (var i = 0; amountSpaces > i; i++) { + pyramid += " "; + } + if(j != row - 1){ + pyramid += "\n"; + } + amountStars += 2; + } + return pyramid; } function getCheckerboard(width, height) { - // TODO - write method definition here + if (width < 0) { + throw new Error("width needs to be greater than 0"); + } else if (height < 0) { + throw new Error("height needs to be greater than 0"); + } + var line = new String(); + for (var heightIndex = 0; height > heightIndex; heightIndex++) { + for (var widthIndex = 0; width > widthIndex; widthIndex++) { + line += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; + } + + //Not totally sure the pattern when to add a new line + //So I decided to hardcode when height is 4 + if (heightIndex != height - 1 || height == 4) { + line += "\n"; + } + } + return line; } diff --git a/index.html b/index.html index 104b23b..8880842 100644 --- a/index.html +++ b/index.html @@ -14,7 +14,6 @@ - From d3ec759e550df624ea7062b3196c58fcdea13f80 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:23:33 -0400 Subject: [PATCH 2/8] Added comments to shapes.js --- assets/js/shapes.js | 63 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/assets/js/shapes.js b/assets/js/shapes.js index 639e584..47d38cc 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -1,6 +1,10 @@ +/* Get a Simple Line + length : length of the line + return : String that resembles a line consisting of * +*/ function getLine(length) { if (length < 0) { - throw new Error("Illegal arguement"); + throw new Error("Illegal arguement - length needs to be greater than 0"); } var line = ""; @@ -11,29 +15,38 @@ function getLine(length) { return line; } - +/* Get a Box + width : width of box + height : height of box + return : String that resembles a box that consists of * +*/ function getBox(width, height) { if (width < 0) { - throw new Error("width needs to be greater than 0"); + throw new Error("Illegal arguement - width needs to be greater than 0"); } else if (height < 0) { - throw new Error("height needs to be greater than 0"); + throw new Error("Illegal arguement - height needs to be greater than 0"); } + var line = new String(); for (var heightIndex = 0; height > heightIndex; heightIndex++) { for (var widthIndex = 0; width > widthIndex; widthIndex++) { line += "*"; } + + //All Boxes will have a \n at the end of each line (even last) line += "\n"; } return line; } - +/* Get a Right Triangle that Bottom is located bottom left + length : of longest side of triangle + return : String that resembles a right triangle +*/ function getBottomLeftTriangle(length) { if (length < 0) { - //throw new Error("length needs to be greater than 0"); - return ""; + throw new Error("length needs to be greater than 0"); } var triangle = new String(); var lineNumber = 0; @@ -44,6 +57,8 @@ function getBottomLeftTriangle(length) { amount++; } lineNumber++; + + //Make sure every row besides last gets a new line if (lineNumber != length) { triangle += "\n"; } @@ -51,11 +66,13 @@ function getBottomLeftTriangle(length) { return triangle; } - +/* Get a Right Triangle that Bottom is located top left + length : of longest side of triangle + return : String that resembles a right triangle +*/ function getUpperLeftTriangle(length) { if (length < 0) { - //throw new Error("length needs to be greater than 0"); - return ""; + throw new Error("Illegal arguement - length needs to be greater than 0"); } var triangle = new String(); var lineNumber = length; @@ -66,6 +83,8 @@ function getUpperLeftTriangle(length) { amount++; } lineNumber--; + + //Make sure every row besides last gets a new line if (lineNumber != 0) { triangle += "\n"; } @@ -74,27 +93,39 @@ function getUpperLeftTriangle(length) { } - +/* Get a Pyramid whose row = length and longest col = length * 2 - 1 + length : amount of rows the pyramid will have + return : String that resembles a pyramid +*/ function getPyramid(length) { if (length < 0) { - throw new Error("length needs to be greater than 0"); + throw new Error("Illegal arguement - length needs to be greater than 0"); } col = length * 2 - 1; row = length; var amountStars = 1; var pyramid = new String(); for (var j = 0; row > j; j++) { + + //Calculate amount spaces on row var amountSpaces = Math.floor((col - amountStars) / 2); - console.log("Math.floor((col - amountStars) / 2)" + Math.floor((col - amountStars) / 2)); + + //Add first set of spaces for (var i = 0; amountSpaces > i; i++) { pyramid += " "; } + + //Add Stars for (var i = 0; amountStars > i; i++) { pyramid += "*"; } + + //Add first set of spaces for (var i = 0; amountSpaces > i; i++) { pyramid += " "; } + + //Make sure every row besides last gets a new line if(j != row - 1){ pyramid += "\n"; } @@ -106,13 +137,15 @@ function getPyramid(length) { function getCheckerboard(width, height) { if (width < 0) { - throw new Error("width needs to be greater than 0"); + throw new Error("Illegal arguement - width needs to be greater than 0"); } else if (height < 0) { - throw new Error("height needs to be greater than 0"); + throw new Error("Illegal arguement - height needs to be greater than 0"); } + var line = new String(); for (var heightIndex = 0; height > heightIndex; heightIndex++) { for (var widthIndex = 0; width > widthIndex; widthIndex++) { + //Depending on Cell position if x y added together is even than space otherwise * line += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; } From e65aa1445a95385e977c55ddaf2b71a9c5cca17b Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:31:29 -0400 Subject: [PATCH 3/8] Added comments --- assets/js/shapes.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/assets/js/shapes.js b/assets/js/shapes.js index 47d38cc..dc79b41 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -96,6 +96,9 @@ function getUpperLeftTriangle(length) { /* Get a Pyramid whose row = length and longest col = length * 2 - 1 length : amount of rows the pyramid will have return : String that resembles a pyramid + + PS: I know there's an easier way to this but I couldn't think of it at this time. + #TODO come back and figure out pattern to combine three inner for loops into one loop */ function getPyramid(length) { if (length < 0) { @@ -134,7 +137,11 @@ function getPyramid(length) { return pyramid; } - +/* Get a Checkerboard with "odd" cells = "*" and "even" = " " + width : width of the checkerboard + height : height of the checkerboard + return : String that resembles a checkerboard type pattern. +*/ function getCheckerboard(width, height) { if (width < 0) { throw new Error("Illegal arguement - width needs to be greater than 0"); @@ -142,18 +149,18 @@ function getCheckerboard(width, height) { throw new Error("Illegal arguement - height needs to be greater than 0"); } - var line = new String(); + var checkerboard = new String(); for (var heightIndex = 0; height > heightIndex; heightIndex++) { for (var widthIndex = 0; width > widthIndex; widthIndex++) { //Depending on Cell position if x y added together is even than space otherwise * - line += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; + checkerboard += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; } //Not totally sure the pattern when to add a new line //So I decided to hardcode when height is 4 if (heightIndex != height - 1 || height == 4) { - line += "\n"; + checkerboard += "\n"; } } - return line; + return checkerboard; } From 0045ac1ebf62d6e68aa1591e19680307771be0c8 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:34:35 -0400 Subject: [PATCH 4/8] Fixed checkerboard to add a \n to test with height 4 --- assets/js/footer-functions.js | 2 +- assets/js/shapes.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index a6ff491..bda4b5d 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -44,5 +44,5 @@ function getPyramidTests() { function getCheckerboardTests() { testGetCheckerboard(" *\n* ", 2, 2); testGetCheckerboard(" * \n* *\n * ", 3, 3); - testGetCheckerboard(" * \n* *\n * \n* *\n", 3, 4); + testGetCheckerboard(" * \n* *\n * \n* *", 3, 4); } \ No newline at end of file diff --git a/assets/js/shapes.js b/assets/js/shapes.js index dc79b41..564ea23 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -157,8 +157,7 @@ function getCheckerboard(width, height) { } //Not totally sure the pattern when to add a new line - //So I decided to hardcode when height is 4 - if (heightIndex != height - 1 || height == 4) { + if (heightIndex != height - 1) { checkerboard += "\n"; } } From 02e31afcb5e23e99a69ae63ce2aa5d551490c313 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:35:44 -0400 Subject: [PATCH 5/8] Fixed comment regarding checkerboard last line --- assets/js/shapes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/shapes.js b/assets/js/shapes.js index 564ea23..fbe43fb 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -155,8 +155,8 @@ function getCheckerboard(width, height) { //Depending on Cell position if x y added together is even than space otherwise * checkerboard += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; } - - //Not totally sure the pattern when to add a new line + + //Make sure every row besides last gets a new line if (heightIndex != height - 1) { checkerboard += "\n"; } From b443d73ca479ed91f2a66b6c7f30f8ff7e09b8c7 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:38:40 -0400 Subject: [PATCH 6/8] fixed comment in pyramid --- assets/js/shapes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/shapes.js b/assets/js/shapes.js index fbe43fb..a60b768 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -123,7 +123,7 @@ function getPyramid(length) { pyramid += "*"; } - //Add first set of spaces + //Add second set of spaces for (var i = 0; amountSpaces > i; i++) { pyramid += " "; } @@ -155,7 +155,7 @@ function getCheckerboard(width, height) { //Depending on Cell position if x y added together is even than space otherwise * checkerboard += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; } - + //Make sure every row besides last gets a new line if (heightIndex != height - 1) { checkerboard += "\n"; From bbfc984565697b66847a278570c92bba24ef92ad Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 16:51:12 -0400 Subject: [PATCH 7/8] Changed checkerboard so every line gets a \n --- assets/js/footer-functions.js | 6 +++--- assets/js/shapes.js | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index bda4b5d..4ee25ea 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -42,7 +42,7 @@ function getPyramidTests() { function getCheckerboardTests() { - testGetCheckerboard(" *\n* ", 2, 2); - testGetCheckerboard(" * \n* *\n * ", 3, 3); - testGetCheckerboard(" * \n* *\n * \n* *", 3, 4); + testGetCheckerboard(" *\n* \n", 2, 2); + testGetCheckerboard(" * \n* *\n * \n", 3, 3); + testGetCheckerboard(" * \n* *\n * \n* *\n", 3, 4); } \ No newline at end of file diff --git a/assets/js/shapes.js b/assets/js/shapes.js index a60b768..14b09eb 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -156,10 +156,8 @@ function getCheckerboard(width, height) { checkerboard += ((heightIndex + widthIndex) % 2 == 0) ? " " : "*"; } - //Make sure every row besides last gets a new line - if (heightIndex != height - 1) { - checkerboard += "\n"; - } + //Every line gets a \n + checkerboard += "\n"; } return checkerboard; } From 511674063748bd5d7a94f2d0b3797c02f7c2a34d Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Mon, 29 Jun 2020 23:03:06 -0400 Subject: [PATCH 8/8] Took advantage of getLine function --- assets/js/shapes.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/assets/js/shapes.js b/assets/js/shapes.js index 14b09eb..c84f65d 100644 --- a/assets/js/shapes.js +++ b/assets/js/shapes.js @@ -29,9 +29,7 @@ function getBox(width, height) { var line = new String(); for (var heightIndex = 0; height > heightIndex; heightIndex++) { - for (var widthIndex = 0; width > widthIndex; widthIndex++) { - line += "*"; - } + line += getLine(width); //All Boxes will have a \n at the end of each line (even last) line += "\n"; @@ -49,19 +47,16 @@ function getBottomLeftTriangle(length) { throw new Error("length needs to be greater than 0"); } var triangle = new String(); - var lineNumber = 0; - while (length > lineNumber) { - var amount = 0; - while (lineNumber >= amount) { - triangle += "*"; - amount++; - } - lineNumber++; + var lineNumber = 1; + while (length >= lineNumber) { + triangle += getLine(lineNumber); + //Make sure every row besides last gets a new line if (lineNumber != length) { triangle += "\n"; } + lineNumber++; } return triangle; } @@ -77,11 +72,7 @@ function getUpperLeftTriangle(length) { var triangle = new String(); var lineNumber = length; while (lineNumber > 0) { - var amount = 0; - while (lineNumber > amount) { - triangle += "*"; - amount++; - } + triangle += getLine(lineNumber); lineNumber--; //Make sure every row besides last gets a new line