From cfb656958601460415c9fd9493a61622740365fa Mon Sep 17 00:00:00 2001 From: gglowe Date: Wed, 25 Nov 2020 15:01:40 -0500 Subject: [PATCH 1/2] added finish main.js with solutions and index.html --- index.html | 11 +++++ main.js | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 index.html create mode 100644 main.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..8a1c6b2 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + + + JS Loops Lab + + + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..4508618 --- /dev/null +++ b/main.js @@ -0,0 +1,117 @@ +// For answers to be checked the other problems will need to be commented out due to variables aleady be declared + + +// Problem 1 + +for (i = 1; i <= 10; i++){ + console.log(i) +} + +// Problem 2 + +for (i = 1; i <= 10; i++) { + console.log(i * i) +} + +// Problem 3 + +let n = prompt('Please enter positive number'); + +for (i = 1; i < n; i++) { + if (i % 2 == 0) { + console.log(i) + } +} + +// Problem 4 + +let n = prompt('Please enter positive number') + n = Number(n) +let m = prompt('Please enter positive number') + m = Number(m) + +let sum = 0 + +while (n < m) { + sum += n; + n++; + } + + alert(sum) + +// started alternative method if n was larger than m + +// if (n < m) { +// sum += n; +// n++; +// } else if (n > m) { +// sum += n - 1; +// n--; +// } + +// Problem 5 + +do { + userInput = prompt('Are we there yet?') +} while (userInput !== 'Yes') + +alert('Good!') + +// Problem 6 + +let rows = 5; + +for (let currentRow = 1; currentRow <= rows; currentRow++) { + let stars = "" + for (let j = 1; j <= currentRow; j++) { + stars += "*"; + } + console.log(stars); +} + +// Solved without nested loop + +// let output = '' + +// for (let i = 1; i < 6; i++) { +// output += '*'; +// console.log(output); +// } + +// Problem 7 + +rows = 4 +columns = 4 + +for (let currentRow = 1; currentRow <= rows; currentRow++) { + let num = "" + for (let currentCol = 1; currentCol <= columns; currentCol++){ + let product = "" + product += currentCol * currentRow + if (product < 10) { + num += ` ${product} |` + } else { + num += ` ${product} |` + } + } + console.log(num) +} + +// Problem 8 + +function timesTable(rows, columns) { + +for (let currentRow = 1; currentRow <= rows; currentRow++) { + let num = "" + for (let currentCol = 1; currentCol <= columns; currentCol++){ + let product = "" + product += currentCol * currentRow + if (product < 10) { + num += ` ${product} |` + } else { + num += ` ${product} |` + } + } + console.log(num) +} +} \ No newline at end of file From fc83fe673029b4dc059f7ce046a0870b7e0c3890 Mon Sep 17 00:00:00 2001 From: gglowe Date: Thu, 26 Nov 2020 11:06:08 -0500 Subject: [PATCH 2/2] fixed problem 7/problem 8 --- main.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/main.js b/main.js index 4508618..bbb2eac 100644 --- a/main.js +++ b/main.js @@ -88,7 +88,10 @@ for (let currentRow = 1; currentRow <= rows; currentRow++) { for (let currentCol = 1; currentCol <= columns; currentCol++){ let product = "" product += currentCol * currentRow - if (product < 10) { + if (currentCol === 1){ + num += `|` + } + if (product < 10 && currentCol > 1) { num += ` ${product} |` } else { num += ` ${product} |` @@ -100,18 +103,20 @@ for (let currentRow = 1; currentRow <= rows; currentRow++) { // Problem 8 function timesTable(rows, columns) { - -for (let currentRow = 1; currentRow <= rows; currentRow++) { - let num = "" - for (let currentCol = 1; currentCol <= columns; currentCol++){ - let product = "" - product += currentCol * currentRow - if (product < 10) { - num += ` ${product} |` - } else { - num += ` ${product} |` + for (let currentRow = 1; currentRow <= rows; currentRow++) { + let num = "" + for (let currentCol = 1; currentCol <= columns; currentCol++){ + let product = "" + product += currentCol * currentRow + if (currentCol === 1){ + num += `|` + } + if (product < 10 && currentCol > 1) { + num += ` ${product} |` + } else { + num += ` ${product} |` + } } + console.log(num) + } } - console.log(num) -} -} \ No newline at end of file