From 1f510453c60068e8bf189577b68637cf409c4fec Mon Sep 17 00:00:00 2001 From: Jerome CHUA Date: Fri, 18 Dec 2020 08:48:52 +0800 Subject: [PATCH 1/4] Complete Let's Draw V1 (Number of Characters) --- script.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..cca5d5d 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,14 @@ +// ---------------------------------------- +// Let's Draw V1 (Number of Characters) +// ---------------------------------------- + var main = function (input) { - var myOutputValue = 'hello world'; + var myOutputValue = "Let's draw:
"; + + // Continue to add 👍 to myOutputValue as long as `i` is not more than the input number + for (var i = 0; i < input; i += 1) { + myOutputValue += '👍'; + } + return myOutputValue; }; From 763b48e5a7f98afcd8adae4b826439af63d6c248 Mon Sep 17 00:00:00 2001 From: Jerome CHUA Date: Fri, 18 Dec 2020 08:57:28 +0800 Subject: [PATCH 2/4] Complete Let's Draw V2 (Square) --- script.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index cca5d5d..e091c81 100644 --- a/script.js +++ b/script.js @@ -1,13 +1,30 @@ // ---------------------------------------- -// Let's Draw V1 (Number of Characters) +// Let's Draw V2 (Square) // ---------------------------------------- var main = function (input) { var myOutputValue = "Let's draw:
"; + // Initialise main counter + var counter = 0; - // Continue to add 👍 to myOutputValue as long as `i` is not more than the input number - for (var i = 0; i < input; i += 1) { - myOutputValue += '👍'; + // Keep running this loop as long as the counter is less than then number input + while (counter < input) { + // Initialise counter for the inner loop, within while loop + var innerCounter = 0; + + // Keep running this inner loop as long as it also less than than number input + while (innerCounter < input) { + // While in this loop, keep adding 👍 input - 1 number of times + myOutputValue += '👍'; + // We update this innerCounter by adding 1 so we do not get stuck in an infinite loop + // this will help the innerCounter to be greater than the input at some point + // breaking us out of the current immediate while loop + innerCounter += 1; + } + // Once broken out of the inner while loop, add a break to go to the next line + myOutputValue += '
'; + // Likewise, this counter is updated to break out of the main loop + counter += 1; } return myOutputValue; From be1830b3677c772216e6fd8050fbc36e1f763328 Mon Sep 17 00:00:00 2001 From: Jerome CHUA Date: Fri, 18 Dec 2020 12:17:29 +0800 Subject: [PATCH 3/4] Complete Let's Draw V3 (Triangle) --- script.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/script.js b/script.js index e091c81..18effb3 100644 --- a/script.js +++ b/script.js @@ -1,29 +1,30 @@ // ---------------------------------------- -// Let's Draw V2 (Square) +// Let's Draw V3 (Triangle) // ---------------------------------------- var main = function (input) { var myOutputValue = "Let's draw:
"; - // Initialise main counter + // Initialise main counter to 0 var counter = 0; - // Keep running this loop as long as the counter is less than then number input + // Run this outer loop as long as counter is less than the user input while (counter < input) { - // Initialise counter for the inner loop, within while loop + // Initialise an inner counter for the inner while loop + // This is initialised to 0 every time we come out of the inner loop var innerCounter = 0; - // Keep running this inner loop as long as it also less than than number input - while (innerCounter < input) { - // While in this loop, keep adding 👍 input - 1 number of times + // As long as counter for inner loop is less than main counter, add 👍 to myOutputValue + // For the first run, it will immediately jump to lines 24 & 25 + while (innerCounter < counter) { myOutputValue += '👍'; - // We update this innerCounter by adding 1 so we do not get stuck in an infinite loop - // this will help the innerCounter to be greater than the input at some point - // breaking us out of the current immediate while loop + // Add 1 to inner counter to exit this inner loop and move back to the main loop innerCounter += 1; } - // Once broken out of the inner while loop, add a break to go to the next line + + // This is part of the outer while loop & adds a
everytime we exit the inner loop myOutputValue += '
'; - // Likewise, this counter is updated to break out of the main loop + // Add 1 to main counter to get closer to user input each time & fulfil the condition + // so as to close the outer while loop counter += 1; } From a822bcf45e2d526c892c9341d7201d6c4ad2bfe3 Mon Sep 17 00:00:00 2001 From: Jerome CHUA Date: Fri, 18 Dec 2020 18:16:09 +0800 Subject: [PATCH 4/4] Complete Let's Draw V4 (Outline Square) --- script.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/script.js b/script.js index 18effb3..f79fd83 100644 --- a/script.js +++ b/script.js @@ -1,32 +1,28 @@ // ---------------------------------------- -// Let's Draw V3 (Triangle) +// Let's Draw V4 (Outline Square) // ---------------------------------------- var main = function (input) { - var myOutputValue = "Let's draw:
"; - // Initialise main counter to 0 + var myOutputValue = "Let's draw:

"; var counter = 0; - // Run this outer loop as long as counter is less than the user input + // As long as counter is less than input, run this loop while (counter < input) { - // Initialise an inner counter for the inner while loop - // This is initialised to 0 every time we come out of the inner loop var innerCounter = 0; - - // As long as counter for inner loop is less than main counter, add 👍 to myOutputValue - // For the first run, it will immediately jump to lines 24 & 25 - while (innerCounter < counter) { - myOutputValue += '👍'; - // Add 1 to inner counter to exit this inner loop and move back to the main loop + // As long as inner counter is less than input, run this loop + while (innerCounter < input) { + /* We get the middle of the square by the following: + ( 1st column || The middle of top row (excl. 1st & last) || Last column || The middle of bottom row (excl .1st & last) */ + if (innerCounter == 0 || counter == 0 || innerCounter == input - 1 || counter == input - 1) { + myOutputValue += '✊'; + } else { + myOutputValue += '👍'; + } + // Update inner counter to exit inner loop to add
to go to next line of drawing innerCounter += 1; } - - // This is part of the outer while loop & adds a
everytime we exit the inner loop myOutputValue += '
'; - // Add 1 to main counter to get closer to user input each time & fulfil the condition - // so as to close the outer while loop counter += 1; } - return myOutputValue; };