Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/advanced/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,57 @@ const nums = [1, 10, 3, 9, 4, 8, 5, 7, 6, 2, -5, -2, -4, -9] // eslint-disable-l
let hasTen = false
let indexOfTen = -1

nums.forEach((x, index) => {
if (x === 10) {
hasTen = true
indexOfTen = index
}
})

// 2. Use a for loop to count how many numbers in the array are divisible by 3
let divisibleByThreeCount = 0
nums.forEach((x) => {
if (x % 3 === 0) {
divisibleByThreeCount += 1
}
})

// 3. use a for loop to find the average of the numbers in the array
let average = 0
nums.forEach((x) => {
average += x
})
average = average / nums.length

// 4. use a for loop to find the largest number in the array
let largest = 0
nums.forEach((x) => {
if (x > largest) {
largest = x
}
})

// 5. use a for loop to find the smallest number in the array
let smallest = 100000
nums.forEach((x) => {
if (x < smallest) {
smallest = x
}
})

// 6. find the median of the numbers in the array
let median = 0

nums.sort(function (a, b) {
return a - b
})

const mid = Math.floor((nums.length - 1) / 2)
median = nums[mid]

console.log(median)
console.log(nums)

module.exports = {
hasTen,
indexOfTen,
Expand Down
65 changes: 65 additions & 0 deletions src/extensions/nested-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,85 @@ const deepThree = []
// Your code here
// }

for (let i = 1; i < 11; i++) {
simpleOne.push(i)
}
// console.log(simpleOne)

// 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 innerArray = []
for (let n = 1; n <= i; n++) {
innerArray.push(i)
}
nestedOne.push(innerArray)
}
// console.log(nestedOne)

// 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 innerArray = []
for (let n = i; n >= START; n--) {
innerArray.push(n)
}
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]],...]

for (let i = START; i <= END; i++) {
const innerArray = []
for (let n = 0; n < i; n++) {
innerArray.push(Array.from({ length: n + 1 }, (_, f) => (f = i)))
}
deepOne.push(innerArray.length === 0 ? [i] : innerArray)
}
// console.log(deepOne)

// 5. As 4, update array 'deepTwo' so that the result is:
// [[[1]],[[1],[1,2]],...]

for (let i = START; i <= END; i++) {
const innerArray = []
for (let n = 0; n < i; n++) {
innerArray.push(Array.from({ length: n + 1 }, (_, f) => f + 1))
}
deepTwo.push(innerArray)
}
// 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]],...]
let testarray = []
for (let i = START; i <= END; i++) {
const innerArray = []
for (let n = 0; n < i; n++) {
innerArray.push(Array.from({ length: n + 1 }, (_, f) => (f + 1) * (f + 1)))
const subarray = innerArray[n]
let sum = 0
subarray.forEach((x) => {
sum += x
})
sum = sum / subarray.length
testarray.push(sum)
testarray = [...new Set(testarray)]

// subarray.pop()
// subarray.push(sum)
// innerArray.pop()
// innerArray.push(testarray)
}

deepThree.push(testarray.map((element) => [element]))
}
console.log(deepThree)
// console.log(testarray)

module.exports = {
START,
Expand Down
17 changes: 17 additions & 0 deletions src/for-loop-and-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@ 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
nums.forEach((element) => {
sum += element
})

// 2. Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...])
const doubledNums = []
nums.forEach((x) => {
doubledNums.push(x * 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((x) => {
word += x
})

// 4. Use a for loop to populate everySecondNum with every second number from the nums array
const everySecondNum = []
nums.forEach((x, index) => {
if (index % 2 === 1) {
everySecondNum.push(x)
}
})

// 5. Use a for loop to populate numsReversed with the numbers from nums in reverse order
const numsReversed = []
nums.forEach((x) => {
numsReversed.unshift(x)
})

// do not change below this line
module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions src/for-loop-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ const countdown = []

// TODO: 1. Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array

numsZeroToThree.push(...Array.from({ length: 4 }, (_, n) => n))
console.log(numsZeroToThree)

// TODO: 2. Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array

numsFiveToTen.push(...Array.from({ length: 6 }, (_, n) => n + 5))
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

evenNums.push(...Array.from({ length: 4 }, (_, n) => n * 2))
console.log(evenNums)

// TODO: 4. Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array

countdown.push(...Array.from({ length: 4 }, (_, n) => 3 - n))
console.log(countdown)

// do not change below this line
module.exports = {
a: numsZeroToThree,
Expand Down