From 99b73fccf38263bcf8035efea0a74f0b8b265cb7 Mon Sep 17 00:00:00 2001 From: awongh Date: Mon, 31 Aug 2020 13:45:32 +0800 Subject: [PATCH 1/3] concentric solution --- script.js | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 0f68729..cb3342e 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,119 @@ // Please declare functions and variables above where they are used. var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; + + // if we arent at the beggining or ending row or + // center for odd number: + + // to draw a straight line for the top + // of a box for an inner box, + // find where the left corner of the box + // begins and stop alternating chars + // and find the right corner of the box and + // begin alternating again + + // otherwise, alternate beggining with the 1st char at the left edge + + // right + // top + // aaaaaaa  + // abbbbbA col == size - row -- 6 == 7 - 1 // switch back to 1st char + // abaaaBa col == size - row -- 5 == 7 - 2 + + // abababa + + // bottom + // abaaaba + // abbbbBa col == row // switch back to 1st char + // aaaaaaa + + // left + // top + // aaaaaaa + // aBbbbba col == row // switch to 2nd char + // abaaaba + + // abababa + + // bottom + // aBaaaba col + 1 == size - row -- 2 == 7 - 4 // switch to 2nd char + // aBbbbba col + 1 == size - row -- 1 == 7 - 5 + // aaaaaaa + + var out = ''; + var size = input; + + var printSmiley; + var alternate; + var rowPosition = 'top' + + var rowCounter = 0; + + while (rowCounter < size) { + var row = rowCounter; + + alternate = true; + + if (row > Math.floor(size / 2)) { + rowPosition = 'bottom' + } + + var columnCounter = 0; + while (columnCounter < size) { + var col = columnCounter; + + // top and bottom + if (row == 0 || row == size - 1) { + alternate = false; + printSmiley = true; + + // center + } else if (row == (Math.floor(size / 2))) { + + // center if even + if (size % 2 == 0 && col == (size - row)) { + alternate = true; + printSmiley = !printSmiley; + } + // upper left + } else if (rowPosition == 'top' && col == row) { + alternate = false; + + // lower right + } else if (rowPosition == 'bottom' && col == row) { + alternate = true; + + // lower left + } else if (rowPosition == 'bottom' && col + 1 == size - row) { + alternate = false; + + // upper right + } else if (rowPosition == 'top' && col == (size - row) && alternate == false) { + alternate = true; + // flip it back, bc we are back to alternating. but alternate wont happen until later + printSmiley = !printSmiley; + } + + if (printSmiley) { + out += '😁'; + } else { + out += '♥️'; + } + + // alternate by switching character + if (alternate == true) { + printSmiley = !printSmiley; + } + columnCounter = columnCounter + 1; + } + + // end of line + out += '
'; + + // reset character to edge + printSmiley = true; + rowCounter = rowCounter + 1; + } + + return out; }; From 4703e4fc7b0cca90966e5322b5c17dcbc56e0afd Mon Sep 17 00:00:00 2001 From: awongh Date: Mon, 31 Aug 2020 13:52:05 +0800 Subject: [PATCH 2/3] comment formatting --- script.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/script.js b/script.js index cb3342e..e67e8b6 100644 --- a/script.js +++ b/script.js @@ -15,28 +15,20 @@ var main = function (input) { // otherwise, alternate beggining with the 1st char at the left edge // right - // top // aaaaaaa  - // abbbbbA col == size - row -- 6 == 7 - 1 // switch back to 1st char + // abbbbbA col == size - row -- 6 == 7 - 1 // top: switch back to 1st char // abaaaBa col == size - row -- 5 == 7 - 2 - // abababa - - // bottom // abaaaba - // abbbbBa col == row // switch back to 1st char + // abbbbBa col == row // bottom: switch back to 1st char // aaaaaaa // left - // top // aaaaaaa - // aBbbbba col == row // switch to 2nd char + // aBbbbba col == row // top: switch to 2nd char // abaaaba - // abababa - - // bottom - // aBaaaba col + 1 == size - row -- 2 == 7 - 4 // switch to 2nd char + // aBaaaba col + 1 == size - row -- 2 == 7 - 4 // bottom: switch to 2nd char // aBbbbba col + 1 == size - row -- 1 == 7 - 5 // aaaaaaa @@ -50,16 +42,20 @@ var main = function (input) { var rowCounter = 0; while (rowCounter < size) { + var row = rowCounter; alternate = true; + // only flip this once when we've gone past the center row if (row > Math.floor(size / 2)) { rowPosition = 'bottom' } var columnCounter = 0; + while (columnCounter < size) { + var col = columnCounter; // top and bottom @@ -67,7 +63,7 @@ var main = function (input) { alternate = false; printSmiley = true; - // center + // center } else if (row == (Math.floor(size / 2))) { // center if even @@ -75,19 +71,20 @@ var main = function (input) { alternate = true; printSmiley = !printSmiley; } - // upper left + + // upper left } else if (rowPosition == 'top' && col == row) { alternate = false; - // lower right + // lower right } else if (rowPosition == 'bottom' && col == row) { alternate = true; - // lower left + // lower left } else if (rowPosition == 'bottom' && col + 1 == size - row) { alternate = false; - // upper right + // upper right } else if (rowPosition == 'top' && col == (size - row) && alternate == false) { alternate = true; // flip it back, bc we are back to alternating. but alternate wont happen until later From aa7a43294b2480b2e15180512e0a047416db7f98 Mon Sep 17 00:00:00 2001 From: awongh Date: Mon, 31 Aug 2020 13:54:25 +0800 Subject: [PATCH 3/3] clarifying syntax --- script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index e67e8b6..c4910a9 100644 --- a/script.js +++ b/script.js @@ -92,9 +92,9 @@ var main = function (input) { } if (printSmiley) { - out += '😁'; + out = out + '😁'; } else { - out += '♥️'; + out = out + '♥️'; } // alternate by switching character @@ -105,9 +105,9 @@ var main = function (input) { } // end of line - out += '
'; + out = out + '
'; - // reset character to edge + // reset character to left edge printSmiley = true; rowCounter = rowCounter + 1; }