Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max Olofsson #297

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

43 changes: 43 additions & 0 deletions src/extensions/nested-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 1; i <= 10; ++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
Expand All @@ -19,18 +22,58 @@ 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 = 1; i <= 10; ++i) {
const toAdd = []
for (let j = 0; j <= i - 1; ++j) toAdd.push(i)
nestedOne.push(toAdd)
}

// 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 toAdd = []
for (let j = i; j >= 1; --j) toAdd.push(j)
nestedTwo.push(toAdd)
}

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

deepOne.push(toAdd)
}

// 5. As 4, update array 'deepTwo' so that the result is:
// [[[1]],[[1],[1,2]],...]
for (let i = 1; i <= 10; ++i) {
const toAdd = []
for (let j = 1; j <= i; ++j) {
const toAdd2 = []
for (let k = 1; k <= j; ++k) toAdd2.push(k)
toAdd.push(toAdd2)
}
deepTwo.push(toAdd)
}

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

module.exports = {
START,
Expand Down
16 changes: 16 additions & 0 deletions src/for-loop-and-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@ 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(num => {
doubledNums.push(num * 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 = 0 ; i < nums.length ; ++i){
if(i % 2 == 0) continue
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 = {
Expand Down
13 changes: 13 additions & 0 deletions src/for-loop-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ 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 = {
Expand Down
Loading