From f6f2690ada97ef76213f23037c09b068f83cfde7 Mon Sep 17 00:00:00 2001 From: TMajlu Date: Tue, 1 Oct 2024 13:05:25 +0200 Subject: [PATCH 1/2] Thomas Nielsen --- .idea/.gitignore | 3 +++ .idea/vcs.xml | 4 ++++ src/for-loop-and-arrays.js | 20 ++++++++++++++++++++ src/for-loop-basic.js | 17 +++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/for-loop-and-arrays.js b/src/for-loop-and-arrays.js index 6635155..a560e95 100644 --- a/src/for-loop-and-arrays.js +++ b/src/for-loop-and-arrays.js @@ -7,17 +7,37 @@ let word = '' // eslint-disable-line prefer-const // 1. Use a for loop to set the sum variable to the sum of all the values in nums +for (let i = 0; i < nums.length; i++) { + sum += nums[i] +} + // 2. Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) const doubledNums = [] +for (let i = 0; i < nums.length; i++) { + doubledNums.push(nums[i] * 2) +} + // 3. Use a for loop to set word variable equal to all the letters in the letters array combined as a single string +for (let i = 0; i < letters.length; i++) { + word += letters[i] +} + // 4. Use a for loop to populate everySecondNum with every second number from the nums array const everySecondNum = [] +for (let i = 1; i < nums.length; i += 2) { + everySecondNum.push(nums[i]) +} + // 5. Use a for loop to populate numsReversed with the numbers from nums in reverse order const numsReversed = [] +for (let i = nums.length - 1; i > -1; i--) { + numsReversed.push(nums[i]) +} + // do not change below this line module.exports = { a: sum, diff --git a/src/for-loop-basic.js b/src/for-loop-basic.js index dc3775a..bebac57 100644 --- a/src/for-loop-basic.js +++ b/src/for-loop-basic.js @@ -5,12 +5,28 @@ const countdown = [] // TODO: 1. Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array +for (let i = 0; i < 4; i++) { + numsZeroToThree.push(i) +} + // TODO: 2. Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array +for (let i = 5; i < 11; i++) { + numsFiveToTen.push(i) +} + // TODO: 3. Write a for loop that adds all the even numbers between 0 and 6 (0, 2, 4, 6) to evenNums +for (let i = 0; i < 7; i += 2) { + evenNums.push(i) +} + // TODO: 4. Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array +for (let i = 3; i > -1; i--) { + countdown.push(i) +} + // do not change below this line module.exports = { a: numsZeroToThree, @@ -18,3 +34,4 @@ module.exports = { c: evenNums, d: countdown } + From e511ef0f2e3a9b14f00d96961ee5bd5ed5eb27bf Mon Sep 17 00:00:00 2001 From: TMajlu Date: Thu, 10 Oct 2024 14:22:19 +0200 Subject: [PATCH 2/2] Thomas Nielsen --- .idea/.gitignore | 3 ++ .idea/js-fundamentals-loops.iml | 9 +++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ src/extensions/nested-loops.js | 60 +++++++++++++++++++++++++++++++++ src/for-loop-and-arrays.js | 20 +++++++++++ src/for-loop-basic.js | 16 +++++++++ 8 files changed, 128 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/js-fundamentals-loops.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/js-fundamentals-loops.iml b/.idea/js-fundamentals-loops.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/js-fundamentals-loops.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d8e0b0e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 1d0044b..7fdc3bf 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -11,6 +11,10 @@ const deepThree = [] // 1. Using a for loop from 1 to 10, add the value of the 'loop index' to the array 'simpleOne' // eg [1,2,3...] +for (let i = START; i <= END; i++) { + simpleOne.push(i) +} + // HINT: in the below loop, the statements and block of code needs to be changed // HINT: in the below loop, the var i represents the loop index // for (let i = 5; i < 7; i++) { @@ -20,18 +24,74 @@ const deepThree = [] // 2. Using nested for loops, add arrays to 'nestedOne' where each array has n copies of the outer 'loop index' // eg [[1],[2,2],...] +for (let i = START; i <= END; i++) { + const nest = [] + for (let j = START; j <= i; j++) { + nest.push(i) + } + nestedOne.push(nest) +} + // 3. As 2, but each array should contain the values from the outer 'loop index' to 1 inclusive. Update array 'nestedTwo' // eg [[1],[2,1],...] +for (let i = START; i <= END; i++) { + const nest = [] + for (let j = i; j >= START; j--) { + nest.push(j) + } + nestedTwo.push(nest) +} + // 4. As 2, but each array should contain arrays from 1 to the outer 'loop index' with the value of the outer 'loop index'. Update array 'deepOne' // eg [[[1]],[[2],[2,2]],...] +for (let i = START; i <= END; i++) { + const nest = [] + for (let j = START; j <= i; j++) { + const nester = [] + for (let k = START; k <= j; k++) { + nester.push(i) + } + nest.push(nester) + } + deepOne.push(nest) +} + // 5. As 4, update array 'deepTwo' so that the result is: // [[[1]],[[1],[1,2]],...] +for (let i = START; i <= END; i++) { + const nest = [] + for (let j = START; j <= i; j++) { + const nester = [] + for (let k = START; k <= j; k++) { + nester.push(k) + } + nest.push(nester) + + } + deepTwo.push(nest) + +} + +console.log(deepTwo) + // 6. As 5, update the array 'deepThree', but the result should be the average of the sum of the squares of the numbers in each array // [[1],[[1],[2.5]],...] +for (let i = START; i <= END; i++) { + const nest = [] + for (let j = START; j <= i; j++) { + const nester = [0] + for (let k = START; k <= j; k++) { + nester[0] += k ** 2 / j + } + nest.push(nester) + } + deepThree.push(nest) +} + module.exports = { START, END, diff --git a/src/for-loop-and-arrays.js b/src/for-loop-and-arrays.js index 6635155..a560e95 100644 --- a/src/for-loop-and-arrays.js +++ b/src/for-loop-and-arrays.js @@ -7,17 +7,37 @@ let word = '' // eslint-disable-line prefer-const // 1. Use a for loop to set the sum variable to the sum of all the values in nums +for (let i = 0; i < nums.length; i++) { + sum += nums[i] +} + // 2. Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) const doubledNums = [] +for (let i = 0; i < nums.length; i++) { + doubledNums.push(nums[i] * 2) +} + // 3. Use a for loop to set word variable equal to all the letters in the letters array combined as a single string +for (let i = 0; i < letters.length; i++) { + word += letters[i] +} + // 4. Use a for loop to populate everySecondNum with every second number from the nums array const everySecondNum = [] +for (let i = 1; i < nums.length; i += 2) { + everySecondNum.push(nums[i]) +} + // 5. Use a for loop to populate numsReversed with the numbers from nums in reverse order const numsReversed = [] +for (let i = nums.length - 1; i > -1; i--) { + numsReversed.push(nums[i]) +} + // do not change below this line module.exports = { a: sum, diff --git a/src/for-loop-basic.js b/src/for-loop-basic.js index dc3775a..e4d0b34 100644 --- a/src/for-loop-basic.js +++ b/src/for-loop-basic.js @@ -5,12 +5,28 @@ const countdown = [] // TODO: 1. Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array +for (let i = 0; i < 4; i++) { + numsZeroToThree.push(i) +} + // TODO: 2. Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array +for (let i = 5; i < 11; i++) { + numsFiveToTen.push(i) +} + // TODO: 3. Write a for loop that adds all the even numbers between 0 and 6 (0, 2, 4, 6) to evenNums +for (let i = 0; i < 7; i += 2) { + evenNums.push(i) +} + // TODO: 4. Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array +for (let i = 3; i > -1; i--) { + countdown.push(i) +} + // do not change below this line module.exports = { a: numsZeroToThree,