From 5c4a2b0d829bf52c2fdcc4374d25abb41cb97da6 Mon Sep 17 00:00:00 2001 From: gremble0 Date: Fri, 20 Sep 2024 15:52:14 +0200 Subject: [PATCH 1/4] Complete core --- src/for-loop-and-arrays.js | 9 ++++++--- src/for-loop-basic.js | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/for-loop-and-arrays.js b/src/for-loop-and-arrays.js index 6635155..ed05c89 100644 --- a/src/for-loop-and-arrays.js +++ b/src/for-loop-and-arrays.js @@ -6,17 +6,20 @@ let word = '' // eslint-disable-line prefer-const // TODO: Add code below this line to make the tests pass // 1. Use a for loop to set the sum variable to the sum of all the values in nums +// NO! +sum = nums.reduce((acc, current) => acc + current, 0) // 2. Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) -const doubledNums = [] +const doubledNums = nums.map((value) => value * 2) // 3. Use a for loop to set word variable equal to all the letters in the letters array combined as a single string +letters.forEach((letter) => (word += letter)) // 4. Use a for loop to populate everySecondNum with every second number from the nums array -const everySecondNum = [] +const everySecondNum = nums.filter((_, ind) => ((ind & 1) === 1 ? true : false)) // 5. Use a for loop to populate numsReversed with the numbers from nums in reverse order -const numsReversed = [] +const numsReversed = nums.reduceRight((acc, current) => acc.concat(current), []) // do not change below this line module.exports = { diff --git a/src/for-loop-basic.js b/src/for-loop-basic.js index dc3775a..47eb8fb 100644 --- a/src/for-loop-basic.js +++ b/src/for-loop-basic.js @@ -4,12 +4,16 @@ const evenNums = [] const countdown = [] // TODO: 1. Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array +for (let i = 0; i <= 3; ++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 <= 10; ++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 <= 6; ++i) if ((i & 1) === 0) 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 >= 0; --i) countdown.push(i) // do not change below this line module.exports = { From e7635f0b2dbf8b847e5f4ad16287710ddde1f3d4 Mon Sep 17 00:00:00 2001 From: gremble0 Date: Fri, 20 Sep 2024 16:23:52 +0200 Subject: [PATCH 2/4] Most of nested-loops.js --- src/extensions/nested-loops.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 1d0044b..42f33ce 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -10,6 +10,7 @@ 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 @@ -19,9 +20,16 @@ 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) nestedOne.push(new Array(i).fill(i)) // 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) { + let builder = new Array(i) + for (let j = START - 1, value = i; j < i; ++j, --value) builder[j] = value + nestedTwo.push(builder) +} +console.log(nestedTwo) // 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]],...] From 6c16c7ef892d1e91f7707bb88d189dec9a8b324e Mon Sep 17 00:00:00 2001 From: gremble0 Date: Mon, 23 Sep 2024 10:50:31 +0200 Subject: [PATCH 3/4] Complete extension --- src/extensions/nested-loops.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 42f33ce..15b2d1b 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -29,16 +29,43 @@ for (let i = START; i <= END; ++i) { for (let j = START - 1, value = i; j < i; ++j, --value) builder[j] = value nestedTwo.push(builder) } -console.log(nestedTwo) // 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++) { + let builder = new Array() + for (let j = START; j <= i; j++) { + builder.push(new Array(j).fill(i)) + } + deepOne.push(builder) +} // 5. As 4, update array 'deepTwo' so that the result is: // [[[1]],[[1],[1,2]],...] +for (let i = START; i <= END; i++) { + let outerArray = new Array() + for (let j = START; j <= i; j++) { + let innerArray = new Array() + for (let k = START; k <= j; k++) innerArray.push(k) + outerArray.push(innerArray) + } + deepTwo.push(outerArray) +} // 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++) { + let outerArray = new Array() + for (let j = START; j <= i; j++) { + let sum = 0 + for (let k = START; k <= j; k++) { + sum += k * k + } + let average = sum / j + outerArray.push([average]) + } + deepThree.push(outerArray) +} module.exports = { START, From 3b9c04309880b4a2f3359401407593e5b2aeb2d0 Mon Sep 17 00:00:00 2001 From: gremble0 Date: Mon, 23 Sep 2024 10:59:33 +0200 Subject: [PATCH 4/4] Complete advanced --- src/advanced/advanced.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/advanced/advanced.js b/src/advanced/advanced.js index 8b2b193..31df41b 100644 --- a/src/advanced/advanced.js +++ b/src/advanced/advanced.js @@ -4,22 +4,30 @@ const nums = [1, 10, 3, 9, 4, 8, 5, 7, 6, 2, -5, -2, -4, -9] // eslint-disable-l // note: use a break statement to exit the loop early if the value is found // to prove you have done this, set the variable indexOfTen to the iteration index when you find 10 let hasTen = false -let indexOfTen = -1 +let indexOfTen = nums.indexOf(10) +if (indexOfTen != -1) hasTen = true // 2. Use a for loop to count how many numbers in the array are divisible by 3 -let divisibleByThreeCount = 0 +let divisibleByThreeCount = nums.filter((i) => i % 3 == 0).length // 3. use a for loop to find the average of the numbers in the array -let average = 0 +let average = nums.reduce((acc, i) => acc + i) / nums.length // 4. use a for loop to find the largest number in the array -let largest = 0 +let largest = nums.reduce((largest, i) => (i > largest ? i : largest), 0) // 5. use a for loop to find the smallest number in the array -let smallest = 100000 +let smallest = nums.reduce( + (smallest, i) => (i < smallest ? i : largest), + Infinity +) // 6. find the median of the numbers in the array -let median = 0 +let median = nums.sort((i1, i2) => { + if (i1 < i2) return -1 + else if (i1 > i2) return 1 + else return 0 +})[nums.length / 2 - 1] module.exports = { hasTen,