Skip to content

Commit

Permalink
Completed all exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
tuvaea committed Sep 23, 2024
1 parent 09459ce commit f2e8f3b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/advanced/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,54 @@ 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) {
indexOfTen = i
hasTen = true
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

for (let i = 0; i < nums.length; i++) {
average += nums[i]
}
average = average / 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

nums.sort((a, b) => a - b)
const medNum = nums.splice(nums.length / 2 - 1, 2)
console.log(medNum)
median = (medNum[0] + medNum[1] - 1) / 2
module.exports = {
hasTen,
indexOfTen,
Expand Down
58 changes: 55 additions & 3 deletions src/extensions/nested-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,77 @@ const deepThree = []

// 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
// for (let i = 5; i < 7; i++) {
// 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 <= 10; i++) {
const temp = []
for (let k = 1; k <= i; k++) {
temp.push(i)
}
nestedOne.push(temp)
}

// 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 <= 10; i++) {
const temp = []
for (let k = i; k > 0; k--) {
temp.push(k)
}
nestedTwo.push(temp)
}

// 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 = 1; i <= 10; i++) {
const temp = []
for (let k = 1; k <= i; k++) {
const temp2 = []
for (let j = 1; j <= k; j++) {
temp2.push(i)
}
temp.push(temp2)
}
deepOne.push(temp)
}

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

for (let i = 1; i <= 10; i++) {
const temp = []
for (let k = 1; k <= i; k++) {
const temp2 = []
for (let j = 1; j <= k; j++) {
temp2.push(j)
}
temp.push(temp2)
}
deepTwo.push(temp)
}

// 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 = 1; i <= 10; i++) {
const temp = []
for (let k = 1; k <= i; k++) {
let sum = 0
for (let j = 1; j <= k; j++) {
sum += j * j
}
temp.push([sum / k])
}
deepThree.push(temp)
}

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

// 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 = 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,
Expand Down
16 changes: 16 additions & 0 deletions src/for-loop-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ 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)
}

// 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)
}

// 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)
}

// 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)
}

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

0 comments on commit f2e8f3b

Please sign in to comment.