From 2d5a058bfe07ea6e4f29e368c80abeb498adb4ab Mon Sep 17 00:00:00 2001 From: Connor Stokes Date: Mon, 23 Sep 2024 14:55:13 +0100 Subject: [PATCH 1/2] all done --- src/advanced/advanced.js | 41 ++++++++++++++++++++++--- src/extensions/nested-loops.js | 55 ++++++++++++++++++++++++++++++++-- src/for-loop-and-arrays.js | 19 +++++++++--- src/for-loop-basic.js | 13 +++++++- 4 files changed, 116 insertions(+), 12 deletions(-) diff --git a/src/advanced/advanced.js b/src/advanced/advanced.js index 8b2b193..6404894 100644 --- a/src/advanced/advanced.js +++ b/src/advanced/advanced.js @@ -5,22 +5,55 @@ const nums = [1, 10, 3, 9, 4, 8, 5, 7, 6, 2, -5, -2, -4, -9] // eslint-disable-l // to prove you have done this, set the variable indexOfTen to the iteration index when you find 10 let hasTen = false let indexOfTen = -1 +for (let i = 0; i < nums.length; i++) { + if (nums[i] === 10) { + hasTen = true; + indexOfTen = i; + break; + } +} + // 2. Use a for loop to count how many numbers in the array are divisible by 3 let divisibleByThreeCount = 0 +for (let i = 0; i < nums.length; i++) { + if (nums[i] % 3 === 0) { + divisibleByThreeCount++; + } +} // 3. use a for loop to find the average of the numbers in the array -let average = 0 +let sum = 0; + +for (let i = 0; i < nums.length; i++) { + sum += nums[i]; +} +let average = sum / nums.length; // 4. use a for loop to find the largest number in the array let largest = 0 - +for (let i = 0; i < nums.length; i++) { + if (nums[i] > largest) { + largest = nums[i]; + } +} // 5. use a for loop to find the smallest number in the array let smallest = 100000 - +for (let i = 0; i < nums.length; i++) { + if (nums[i] < smallest) { + smallest = nums[i]; + } +} // 6. find the median of the numbers in the array let median = 0 - +let sortedNums = nums.slice().sort((a, b) => a - b); +if (sortedNums.length % 2 === 0) { + let mid1 = sortedNums[sortedNums.length / 2 - 1]; + let mid2 = sortedNums[sortedNums.length / 2]; + median = (mid1 + mid2) / 2; +} else { + median = sortedNums[Math.floor(sortedNums.length / 2)]; +} module.exports = { hasTen, indexOfTen, diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 1d0044b..02ed9d2 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -10,6 +10,9 @@ 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,18 +22,64 @@ 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 tempArray = []; + for (let j = 1; j <= i; j++) { + tempArray.push(i); + } + nestedOne.push(tempArray); +} // 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 tempArray = []; + for (let j = i; j >= 1; j--) { + tempArray.push(j); + } + nestedTwo.push(tempArray); +} // 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 outerArray = []; + for (let j = 1; j <= i; j++) { + const innerArray = []; + for (let k = 1; k <= j; k++) { + innerArray.push(i); + } + outerArray.push(innerArray); + } + deepOne.push(outerArray); +} // 5. As 4, update array 'deepTwo' so that the result is: // [[[1]],[[1],[1,2]],...] - +for (let i = START; i <= END; i++) { + const outerArray = []; + for (let j = 1; j <= i; j++) { + const innerArray = []; + for (let k = 1; 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++) { + const outerArray = []; + for (let j = 1; j <= i; j++) { + const sumOfSquares = []; + let sum = 0; + for (let k = 1; k <= j; k++) { + sum += k * k; + } + const average = sum / j; + outerArray.push(average); + } + deepThree.push(outerArray); +} module.exports = { START, diff --git a/src/for-loop-and-arrays.js b/src/for-loop-and-arrays.js index 6635155..11d1d48 100644 --- a/src/for-loop-and-arrays.js +++ b/src/for-loop-and-arrays.js @@ -6,18 +6,29 @@ 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 +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 >= 0; 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..20d0afb 100644 --- a/src/for-loop-basic.js +++ b/src/for-loop-basic.js @@ -4,12 +4,23 @@ 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 += 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 >= 0; i--) { + countdown.push(i); +} // do not change below this line module.exports = { From 84ac20d397343b37cbd1adcdffc138de23a8f58b Mon Sep 17 00:00:00 2001 From: Connor Stokes Date: Mon, 23 Sep 2024 15:03:44 +0100 Subject: [PATCH 2/2] fixd --- src/advanced/advanced.js | 5 +++-- src/extensions/nested-loops.js | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/advanced/advanced.js b/src/advanced/advanced.js index 6404894..e9e7581 100644 --- a/src/advanced/advanced.js +++ b/src/advanced/advanced.js @@ -49,11 +49,12 @@ let median = 0 let sortedNums = nums.slice().sort((a, b) => a - b); if (sortedNums.length % 2 === 0) { let mid1 = sortedNums[sortedNums.length / 2 - 1]; - let mid2 = sortedNums[sortedNums.length / 2]; - median = (mid1 + mid2) / 2; + median = mid1; } else { median = sortedNums[Math.floor(sortedNums.length / 2)]; } + + module.exports = { hasTen, indexOfTen, diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 02ed9d2..cd7edf2 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -76,11 +76,12 @@ for (let i = START; i <= END; i++) { sum += k * k; } const average = sum / j; - outerArray.push(average); + outerArray.push([average]); // Wrap the average in an array } deepThree.push(outerArray); } + module.exports = { START, END,