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
45 changes: 43 additions & 2 deletions src/advanced/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,61 @@ 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

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

nums.forEach((num) => {
if (num % 3 === 0) {
divisibleByThreeCount++
}
})

// 3. use a for loop to find the average of the numbers in the array
let average = 0

nums.forEach((num) => {
average += num
})

average = average / nums.length

// 4. use a for loop to find the largest number in the array
let largest = 0
let largest = nums[0]

for (let i = 1; i < nums.length; i++) {
if (largest < nums[i]) {
largest = nums[i]
}
}

// 5. use a for loop to find the smallest number in the array
let smallest = 100000
let smallest = nums[0]

for (let i = 0; i < nums.length; i++) {
if (smallest > nums[i]) {
smallest = nums[i]
}
}

// 6. find the median of the numbers in the array
let median = 0
nums.sort((a, b) => a - b)

const middle = Math.floor(nums.length / 2)

if (nums.length % 2 === 0) {
median = nums[middle - 1]
} else {
median = nums[middle]
}

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

for (let i = START; i <= END; 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 j = 0; j < i; j++) {
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 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]],...]
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++) {
let sumOfSquares = 0

// Sum the squares of numbers from 1 to j
for (let k = 1; k <= j; k++) {
sumOfSquares += k * k
}

// Calculate the average of the sum of the squares
const average = [sumOfSquares / j]

// Push the average value instead of the array of numbers
outerArray.push(average)
}

deepThree.push(outerArray)
}

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

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 = []

nums.forEach((element) => {
doubledNums.push(element * 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
letters.forEach((letter) => {
word += letter
})

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,
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 @@ -4,12 +4,24 @@ 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)

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 <= 10; 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 <= 6; i++) if (i % 2 === 0) 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 >= 0; i--) countdown.push(i)

console.log(countdown)

// do not change below this line
module.exports = {
Expand Down