From 5933227bf317d5ef764f6faa6aae172e636700de Mon Sep 17 00:00:00 2001 From: alvinljt Date: Fri, 28 Aug 2020 17:54:48 +0800 Subject: [PATCH 1/8] number of characters --- package.json | 2 +- script.js | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 90af81e..da8bab3 100644 --- a/package.json +++ b/package.json @@ -21,4 +21,4 @@ "eslint-config-airbnb-base": "^14.2.0", "eslint-plugin-import": "^2.22.0" } -} \ No newline at end of file +} diff --git a/script.js b/script.js index 0f68729..717380e 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,11 @@ // Please declare functions and variables above where they are used. var main = function (input) { - var myOutputValue = 'hello world'; + var counter = 0; + var myOutputValue = ''; + while (counter < input) { + myOutputValue += '😊'; + counter += 1; + } return myOutputValue; }; From 37ddae666b576b4e7e0c7fe3caa49148009cd7f0 Mon Sep 17 00:00:00 2001 From: alvinljt Date: Fri, 28 Aug 2020 18:01:49 +0800 Subject: [PATCH 2/8] square --- script.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index 717380e..e31c7d9 100644 --- a/script.js +++ b/script.js @@ -1,11 +1,17 @@ // Please declare functions and variables above where they are used. var main = function (input) { - var counter = 0; + var rowCounter = 0; + var columnCounter; var myOutputValue = ''; - while (counter < input) { - myOutputValue += '😊'; - counter += 1; + while (rowCounter < input) { + columnCounter = 0; + while (columnCounter < input) { + myOutputValue += '😀'; + columnCounter += 1; + } + myOutputValue += '
'; + rowCounter += 1; } return myOutputValue; }; From 162f2300c3068689cd22fc00e693c59f6a941f38 Mon Sep 17 00:00:00 2001 From: alvinljt Date: Fri, 28 Aug 2020 18:05:45 +0800 Subject: [PATCH 3/8] triangle --- script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index e31c7d9..ed5694c 100644 --- a/script.js +++ b/script.js @@ -4,9 +4,11 @@ var main = function (input) { var rowCounter = 0; var columnCounter; var myOutputValue = ''; + var numOfColumns; while (rowCounter < input) { columnCounter = 0; - while (columnCounter < input) { + numOfColumns = rowCounter + 1; + while (columnCounter < numOfColumns) { myOutputValue += '😀'; columnCounter += 1; } From a127a301d1f7269fa05e375e24d9ab467c00d0ae Mon Sep 17 00:00:00 2001 From: alvinljt Date: Sat, 29 Aug 2020 10:11:30 +0800 Subject: [PATCH 4/8] modes --- script.js | 53 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/script.js b/script.js index ed5694c..e98bcfb 100644 --- a/script.js +++ b/script.js @@ -1,19 +1,52 @@ -// Please declare functions and variables above where they are used. +// Initial mode is shape entering mode +var mode = 'shape entering mode'; +// shapeMode to record the current shape the game is using +var shapeMode; var main = function (input) { var rowCounter = 0; var columnCounter; - var myOutputValue = ''; var numOfColumns; - while (rowCounter < input) { - columnCounter = 0; - numOfColumns = rowCounter + 1; - while (columnCounter < numOfColumns) { - myOutputValue += '😀'; - columnCounter += 1; + var myOutputValue = ''; + + // changing the mode according to the shape user inputs want + if (mode == 'shape entering mode') { + if (input == 'square') { + shapeMode = 'square'; + mode = 'shape mode'; + myOutputValue = 'please enter number of dimensions'; + } else if (input == 'triangle') { + shapeMode = 'triangle'; + mode = 'shape mode'; + myOutputValue = 'please enter number of dimensions'; + } else { + myOutputValue = 'you have entered an invalid input. Please enter square or triangle.'; + } + } else if (mode == 'shape mode') { + if (shapeMode == 'square') { + // game logic for square shapeMode + while (rowCounter < input) { + columnCounter = 0; + while (columnCounter < input) { + myOutputValue += '😀'; + columnCounter += 1; + } + myOutputValue += '
'; + rowCounter += 1; + } + } else if (shapeMode == 'triangle') { + // game logic for triangle shapeMode + while (rowCounter < input) { + columnCounter = 0; + numOfColumns = rowCounter + 1; + while (columnCounter < numOfColumns) { + myOutputValue += '😀'; + columnCounter += 1; + } + myOutputValue += '
'; + rowCounter += 1; + } } - myOutputValue += '
'; - rowCounter += 1; } return myOutputValue; }; From a32c6e40deee2a78b25e03010a355b249ebbfecf Mon Sep 17 00:00:00 2001 From: alvinljt Date: Sat, 29 Aug 2020 10:26:27 +0800 Subject: [PATCH 5/8] upside down triangle --- script.js | 53 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/script.js b/script.js index e98bcfb..7d53dca 100644 --- a/script.js +++ b/script.js @@ -1,52 +1,19 @@ -// Initial mode is shape entering mode -var mode = 'shape entering mode'; -// shapeMode to record the current shape the game is using -var shapeMode; - var main = function (input) { var rowCounter = 0; - var columnCounter; + var columnCounter = input; var numOfColumns; var myOutputValue = ''; - // changing the mode according to the shape user inputs want - if (mode == 'shape entering mode') { - if (input == 'square') { - shapeMode = 'square'; - mode = 'shape mode'; - myOutputValue = 'please enter number of dimensions'; - } else if (input == 'triangle') { - shapeMode = 'triangle'; - mode = 'shape mode'; - myOutputValue = 'please enter number of dimensions'; - } else { - myOutputValue = 'you have entered an invalid input. Please enter square or triangle.'; - } - } else if (mode == 'shape mode') { - if (shapeMode == 'square') { - // game logic for square shapeMode - while (rowCounter < input) { - columnCounter = 0; - while (columnCounter < input) { - myOutputValue += '😀'; - columnCounter += 1; - } - myOutputValue += '
'; - rowCounter += 1; - } - } else if (shapeMode == 'triangle') { - // game logic for triangle shapeMode - while (rowCounter < input) { - columnCounter = 0; - numOfColumns = rowCounter + 1; - while (columnCounter < numOfColumns) { - myOutputValue += '😀'; - columnCounter += 1; - } - myOutputValue += '
'; - rowCounter += 1; - } + // game logic for upside down triangle + while (rowCounter < input) { + numOfColumns = columnCounter; + while (numOfColumns > 0) { + myOutputValue += '😀'; + numOfColumns -= 1; } + myOutputValue += '
'; + rowCounter += 1; + columnCounter -= 1; } return myOutputValue; }; From 23b3082b459342f83661db3913c12c633cef2ed1 Mon Sep 17 00:00:00 2001 From: alvinljt Date: Sat, 29 Aug 2020 10:39:28 +0800 Subject: [PATCH 6/8] outline square --- script.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/script.js b/script.js index 7d53dca..3ab37c3 100644 --- a/script.js +++ b/script.js @@ -1,19 +1,29 @@ var main = function (input) { var rowCounter = 0; - var columnCounter = input; - var numOfColumns; + var columnCounter; var myOutputValue = ''; - // game logic for upside down triangle + // game logic for outline square while (rowCounter < input) { - numOfColumns = columnCounter; - while (numOfColumns > 0) { - myOutputValue += '😀'; - numOfColumns -= 1; + columnCounter = 0; + if (rowCounter == 0 || rowCounter == (input - 1)) { + while (columnCounter < input) { + myOutputValue += '😀'; + columnCounter += 1; + } + } else { + while (columnCounter < input) { + if (columnCounter == 0 || columnCounter == (input - 1)) { + myOutputValue += '😀'; + columnCounter += 1; + } else { + myOutputValue += '👻'; + columnCounter += 1; + } + } } myOutputValue += '
'; rowCounter += 1; - columnCounter -= 1; } return myOutputValue; }; From 2e1889586d8fc38d25df52df1b10870d9b161028 Mon Sep 17 00:00:00 2001 From: alvinljt Date: Sat, 29 Aug 2020 11:13:08 +0800 Subject: [PATCH 7/8] center square --- script.js | 60 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/script.js b/script.js index 3ab37c3..4bfb849 100644 --- a/script.js +++ b/script.js @@ -2,28 +2,56 @@ var main = function (input) { var rowCounter = 0; var columnCounter; var myOutputValue = ''; - - // game logic for outline square - while (rowCounter < input) { - columnCounter = 0; - if (rowCounter == 0 || rowCounter == (input - 1)) { - while (columnCounter < input) { - myOutputValue += '😀'; - columnCounter += 1; - } + var checkInput = input; + var getCanPlayOrNot = function () { + var result = checkInput % 2; + var answer; + if (result == 1) { + answer = 'yes'; } else { - while (columnCounter < input) { - if (columnCounter == 0 || columnCounter == (input - 1)) { + answer = 'no'; + } + return answer; + }; + var canPlayOrNot = getCanPlayOrNot(); + + if (canPlayOrNot == 'no') { + // cannot play game + myOutputValue = 'Please enter an odd number dimension.'; + } else if (canPlayOrNot == 'yes') { + // can play game + // to determine which column and row is center of square + var centerOfSquare = ((parseInt(input) + 1) / 2) - 1; + console.log(centerOfSquare); + + // logic for drawing the square + while (rowCounter < input) { + columnCounter = 0; + // prints border of square + if (rowCounter == 0 || rowCounter == (input - 1)) { + while (columnCounter < input) { myOutputValue += '😀'; columnCounter += 1; - } else { - myOutputValue += '👻'; - columnCounter += 1; + } + } else { + // prints inside of square + while (columnCounter < input) { + if (columnCounter == 0 || columnCounter == (input - 1)) { + myOutputValue += '😀'; + columnCounter += 1; + } else if (columnCounter == centerOfSquare && rowCounter == centerOfSquare) { + // prints center of square + myOutputValue += '😏'; + columnCounter += 1; + } else { + myOutputValue += '👻'; + columnCounter += 1; + } } } + myOutputValue += '
'; + rowCounter += 1; } - myOutputValue += '
'; - rowCounter += 1; } return myOutputValue; }; From e99fe7c9fb9c8d53bfb6819f97b2bc41a19abf2f Mon Sep 17 00:00:00 2001 From: alvinljt Date: Sun, 30 Aug 2020 01:32:54 +0800 Subject: [PATCH 8/8] created rings function and added modes and codes for previous sections --- script.js | 390 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 377 insertions(+), 13 deletions(-) diff --git a/script.js b/script.js index 4bfb849..8e112a4 100644 --- a/script.js +++ b/script.js @@ -1,8 +1,225 @@ -var main = function (input) { +// rings function +var getRings = function (dimensions) { + var row = 0; + var column = 0; + var alternateEmoji = ''; + var repeatingEmoji = ''; + var myOutputValue = ''; + var index; + var indexToStopRepeatingEmojis; + var indexToStopAlternatingEmojis; + console.log('No. of dimensions: ' + dimensions); + if ((dimensions % 2) == 0) { + // ring game logic for even dimensions + while (row < (dimensions / 2)) { + // print the emojis for the upper half rows + alternateEmoji = '😀'; + repeatingEmoji = '😀'; + index = 0; + indexToStopAlternatingEmojis = column; + while (index < indexToStopAlternatingEmojis) { + // print the alternating emojis on the left side of the borders + myOutputValue += alternateEmoji; + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + index += 1; + } + + // determine which repeating emoji to use for that row + repeatingEmoji = alternateEmoji; + + // determine at which index to stop repeating emojis for that row + indexToStopRepeatingEmojis = (dimensions - column); + + while (index < indexToStopRepeatingEmojis) { + // print the top borders of the squares + myOutputValue += repeatingEmoji; + index += 1; + } + + while (index < dimensions) { + // to print the alternating emojis to the right side of the square border + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + myOutputValue += alternateEmoji; + index += 1; + } + myOutputValue += '
'; + row += 1; + column += 1; + } + + console.log('you are at column index: ' + column); + + indexToStopAlternatingEmojis = column; + while (row < dimensions) { + // print emojis for lower half rows + alternateEmoji = '😀'; + repeatingEmoji = '😀'; + index = 0; + indexToStopAlternatingEmojis -= 1; + while (index < indexToStopAlternatingEmojis) { + // print the alternating emojis on the left side of the borders + myOutputValue += alternateEmoji; + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + index += 1; + } + + // determine which repeating emoji to use for that row + repeatingEmoji = alternateEmoji; + + // determine at which index to stop repeating emojis for that row + indexToStopRepeatingEmojis = (column + 1); + + while (index < indexToStopRepeatingEmojis) { + // print the bottom borders of the squares + myOutputValue += repeatingEmoji; + index += 1; + } + + while (index < dimensions) { + // to print the alternating emojis to the right side of the square border + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + myOutputValue += alternateEmoji; + index += 1; + } + myOutputValue += '
'; + row += 1; + column += 1; + } + } else { + // rings game logic for odd dimensions + while (row < (Math.floor(dimensions / 2))) { + // print the emojis for the upper half rows + alternateEmoji = '😀'; + repeatingEmoji = '😀'; + index = 0; + indexToStopAlternatingEmojis = column; + while (index < indexToStopAlternatingEmojis) { + // print the alternating emojis on the left side of the borders + myOutputValue += alternateEmoji; + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + index += 1; + } + + // determine which repeating emoji to use for that row + repeatingEmoji = alternateEmoji; + + // determine at which index to stop repeating emojis for that row + indexToStopRepeatingEmojis = (dimensions - column); + + while (index < indexToStopRepeatingEmojis) { + // print the top borders of the squares + myOutputValue += repeatingEmoji; + index += 1; + } + + while (index < dimensions) { + // to print the alternating emojis to the right side of the square border + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + myOutputValue += alternateEmoji; + index += 1; + } + myOutputValue += '
'; + row += 1; + column += 1; + } + + console.log('you are at column index: ' + column); + + // print alternating emojis for middle row + index = 0; + alternateEmoji = '😀'; + while (index < dimensions) { + myOutputValue += alternateEmoji; + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + index += 1; + } + myOutputValue += '
'; + row += 1; + column += 1; + console.log('you are at column index: ' + column); + + indexToStopAlternatingEmojis = (column - 1); + while (row < dimensions) { + // print emojis for lower half rows + alternateEmoji = '😀'; + repeatingEmoji = '😀'; + index = 0; + indexToStopAlternatingEmojis -= 1; + while (index < indexToStopAlternatingEmojis) { + // print the alternating emojis on the left side of the borders + myOutputValue += alternateEmoji; + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + index += 1; + } + + // determine which repeating emoji to use for that row + repeatingEmoji = alternateEmoji; + + // determine at which index to stop repeating emojis for that row + indexToStopRepeatingEmojis = (column + 1); + + while (index < indexToStopRepeatingEmojis) { + // print the bottom borders of the squares + myOutputValue += repeatingEmoji; + index += 1; + } + + while (index < dimensions) { + // to print the alternating emojis to the right side of the square border + if (alternateEmoji == '😀') { + alternateEmoji = '👻'; + } else { + alternateEmoji = '😀'; + } + myOutputValue += alternateEmoji; + index += 1; + } + myOutputValue += '
'; + row += 1; + column += 1; + } + } + return myOutputValue; +}; + +// center square function +var getCenterSquare = function (dimensions) { var rowCounter = 0; var columnCounter; var myOutputValue = ''; - var checkInput = input; + var checkInput = dimensions; var getCanPlayOrNot = function () { var result = checkInput % 2; var answer; @@ -16,31 +233,31 @@ var main = function (input) { var canPlayOrNot = getCanPlayOrNot(); if (canPlayOrNot == 'no') { - // cannot play game + // cannot play game myOutputValue = 'Please enter an odd number dimension.'; } else if (canPlayOrNot == 'yes') { - // can play game - // to determine which column and row is center of square - var centerOfSquare = ((parseInt(input) + 1) / 2) - 1; + // can play game + // to determine which column and row is center of square + var centerOfSquare = ((parseInt(dimensions) + 1) / 2) - 1; console.log(centerOfSquare); // logic for drawing the square - while (rowCounter < input) { + while (rowCounter < dimensions) { columnCounter = 0; // prints border of square - if (rowCounter == 0 || rowCounter == (input - 1)) { - while (columnCounter < input) { + if (rowCounter == 0 || rowCounter == (dimensions - 1)) { + while (columnCounter < dimensions) { myOutputValue += '😀'; columnCounter += 1; } } else { - // prints inside of square - while (columnCounter < input) { - if (columnCounter == 0 || columnCounter == (input - 1)) { + // prints inside of square + while (columnCounter < dimensions) { + if (columnCounter == 0 || columnCounter == (dimensions - 1)) { myOutputValue += '😀'; columnCounter += 1; } else if (columnCounter == centerOfSquare && rowCounter == centerOfSquare) { - // prints center of square + // prints center of square myOutputValue += '😏'; columnCounter += 1; } else { @@ -55,3 +272,150 @@ var main = function (input) { } return myOutputValue; }; + +// outline square function +var getOutlineSquare = function (dimensions) { + var rowCounter = 0; + var columnCounter; + var myOutputValue = ''; + // game logic for outline square + while (rowCounter < dimensions) { + columnCounter = 0; + if (rowCounter == 0 || rowCounter == (dimensions - 1)) { + while (columnCounter < dimensions) { + myOutputValue += '😀'; + columnCounter += 1; + } + } else { + while (columnCounter < dimensions) { + if (columnCounter == 0 || columnCounter == (dimensions - 1)) { + myOutputValue += '😀'; + columnCounter += 1; + } else { + myOutputValue += '👻'; + columnCounter += 1; + } + } + } + myOutputValue += '
'; + rowCounter += 1; + } + return myOutputValue; +}; + +// upside down triangle function +var getUpsideDownTriangle = function (dimensions) { + var rowCounter = 0; + var columnCounter = dimensions; + var numOfColumns; + var myOutputValue = ''; + // game logic for upside down triangle + while (rowCounter < dimensions) { + numOfColumns = columnCounter; + while (numOfColumns > 0) { + myOutputValue += '😀'; + numOfColumns -= 1; + } + myOutputValue += '
'; + rowCounter += 1; + columnCounter -= 1; + } + return myOutputValue; +}; + +// triangle function +var getTriangle = function (dimensions) { + var rowCounter = 0; + var columnCounter; + var myOutputValue = ''; + var numOfColumns; + while (rowCounter < dimensions) { + columnCounter = 0; + numOfColumns = rowCounter + 1; + while (columnCounter < numOfColumns) { + myOutputValue += '😀'; + columnCounter += 1; + } + myOutputValue += '
'; + rowCounter += 1; + } + return myOutputValue; +}; + +// square function +var getSquare = function (dimensions) { + var rowCounter = 0; + var columnCounter; + var myOutputValue = ''; + while (rowCounter < dimensions) { + columnCounter = 0; + while (columnCounter < dimensions) { + myOutputValue += '😀'; + columnCounter += 1; + } + myOutputValue += '
'; + rowCounter += 1; + } + return myOutputValue; +}; + +// number of characters function +var getNumberOfCharacters = function (dimensions) { + var counter = 0; + var myOutputValue = ''; + while (counter < dimensions) { + myOutputValue += '😊'; + counter += 1; + } + return myOutputValue; +}; + +// Initial mode when brower loads +var mode = 'game choosing mode'; + +var main = function (input) { + var myOutputValue; + // changing the mode according to what the user wants + if (mode == 'game choosing mode') { + if (input == 'number of characters') { + mode = 'number of characters'; + } else if (input == 'square') { + mode = 'square'; + } else if (input == 'triangle') { + mode = 'triangle'; + } else if (input == 'upside down triangle') { + mode = 'upside down triangle'; + } else if (input == 'outline square') { + mode = 'outline square'; + } else if (input == 'center square') { + mode = 'center square'; + } else if (input == 'rings') { + mode = 'rings'; + } + + myOutputValue = 'please enter number of dimensions'; + } else if (mode == 'number of characters') { + var numberOfCharacters = getNumberOfCharacters(input); + myOutputValue = numberOfCharacters; + } else if (mode == 'square') { + var square = getSquare(input); + myOutputValue = square; + } else if (mode == 'triangle') { + var triangle = getTriangle(input); + myOutputValue = triangle; + } else if (mode == 'upside down triangle') { + var upsideDownTriangle = getUpsideDownTriangle(input); + myOutputValue = upsideDownTriangle; + } else if (mode == 'outline square') { + var outlineSquare = getOutlineSquare(input); + myOutputValue = outlineSquare; + } else if (mode == 'center square') { + var centerSquare = getCenterSquare(input); + myOutputValue = centerSquare; + } else if (mode == 'rings') { + var rings = getRings(input); + myOutputValue = rings; + } + + return myOutputValue; +};