From 49fd2fa36f326c77b30cd3b94d71ba998cf6ba86 Mon Sep 17 00:00:00 2001 From: Ali Haider Khan Date: Tue, 24 Sep 2024 11:02:02 +0200 Subject: [PATCH] done with core and 1/2 extensions --- src/advanced/advanced.js | 14 +++++++------- src/example.js | 2 ++ src/extensions/nested-loops.js | 22 ++++++++++++++++++++++ src/for-loop-and-arrays.js | 29 +++++++++++++++++++++++++++++ src/for-loop-basic.js | 24 ++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/advanced/advanced.js b/src/advanced/advanced.js index 8b2b193..439350c 100644 --- a/src/advanced/advanced.js +++ b/src/advanced/advanced.js @@ -3,23 +3,23 @@ const nums = [1, 10, 3, 9, 4, 8, 5, 7, 6, 2, -5, -2, -4, -9] // eslint-disable-l // 1. Use a for loop to set the variable hasTen to true if the array contains the value 10 // 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 +const hasTen = false +const indexOfTen = -1 // 2. Use a for loop to count how many numbers in the array are divisible by 3 -let divisibleByThreeCount = 0 +const divisibleByThreeCount = 0 // 3. use a for loop to find the average of the numbers in the array -let average = 0 +const average = 0 // 4. use a for loop to find the largest number in the array -let largest = 0 +const largest = 0 // 5. use a for loop to find the smallest number in the array -let smallest = 100000 +const smallest = 100000 // 6. find the median of the numbers in the array -let median = 0 +const median = 0 module.exports = { hasTen, diff --git a/src/example.js b/src/example.js index b194393..90e3177 100644 --- a/src/example.js +++ b/src/example.js @@ -17,3 +17,5 @@ module.exports = { a: nums, b: nums3 } + +// done diff --git a/src/extensions/nested-loops.js b/src/extensions/nested-loops.js index 1d0044b..537c5ff 100644 --- a/src/extensions/nested-loops.js +++ b/src/extensions/nested-loops.js @@ -17,12 +17,34 @@ const deepThree = [] // Your code here // } +for (let i = 1; i < 11; i++) { + simpleOne.push(i) +} + // 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 = 1; i < 11; i++) { + const innerarray = [] + for (let j = 0; j < i; j++) { + innerarray.push(i) + } + nestedOne.push(innerarray) +} + // 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 = 1; i < 11; i++) { + const innerarray = [] + for (let j = i; j >= 1; j--) { + innerarray.push(j) + } + nestedTwo.push(innerarray) +} + +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]],...] diff --git a/src/for-loop-and-arrays.js b/src/for-loop-and-arrays.js index 6635155..d22defb 100644 --- a/src/for-loop-and-arrays.js +++ b/src/for-loop-and-arrays.js @@ -7,17 +7,46 @@ 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] +} + +console.log(sum) + // 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[i] = nums[i] * 2 +} +console.log(doubledNums) + // 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] +} + +console.log(word) + // 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]) +} + +console.log(everySecondNum) + // 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]) +} + +console.log(numsReversed) + // 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..4187cde 100644 --- a/src/for-loop-basic.js +++ b/src/for-loop-basic.js @@ -5,12 +5,36 @@ 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) +} + +console.log(numsZeroToThree) + // 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) +} + +console.log(numsFiveToTen) + // 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) +} + +console.log(evenNums) + // 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) +} + +console.log(countdown) + // do not change below this line module.exports = { a: numsZeroToThree,