Skip to content

Commit 3ba2581

Browse files
committed
Add solution to challenges of week 3
1 parent 6c0fd03 commit 3ba2581

5 files changed

Lines changed: 52 additions & 17 deletions

File tree

Week2/prep-exercises/2-experiments/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ function runExperiment(sampleSize) {
2020

2121
function main() {
2222
const sampleSizes = [100, 1000, 1000000];
23-
const allResults = [];
2423

2524
for(const sampleSize of sampleSizes){
26-
const experimentResults = runExperiment(sampleSize);
27-
allResults.push([experimentResults, sampleSize]);
25+
console.log(runExperiment(sampleSize), sampleSize);
2826
}
29-
return allResults;
3027
}
3128

32-
const results = main();
33-
34-
for(const [experimentResults, sampleSize] of results){
35-
console.log(experimentResults, sampleSize);
36-
}
29+
main();

Week3/challenges/1-sum-entries.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ Once you have found those numbers, multiply the numbers and store the result of
99

1010
const list = [1721, 979, 366, 299, 675, 1456];
1111
let result;
12-
13-
// Write your code here
1412

13+
list.forEach((number, ind) => {
14+
for(let i= ind+1; i<list.length; i++){
15+
if(number + list[i] === 2020)
16+
return result = number*list[i]
17+
}
18+
})
1519

1620
// TEST CODE, do not change
1721
console.assert(result === 514579, `The result is not correct, it is ${result}, but should be 514579`);

Week3/challenges/2-sum-three-entries.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ Once you have found those numbers, multiply the numbers and store the result of
1010
const list = [1721, 979, 366, 299, 675, 1456];
1111
let result;
1212

13-
// Write your code here
14-
13+
list.forEach((number, ind) => {
14+
for(let i= ind+1; i<list.length; i++){
15+
for(let j = i+1; j<list.length; j++){
16+
if(number + list[i] + list[j] === 2020)
17+
return result = number*list[i]*list[j]
18+
}
19+
}
20+
})
1521

1622
// TEST CODE, do not change
1723
console.assert(result === 241861950, `The result is not correct, it is ${result}, but should be 241861950`);

Week3/challenges/3-password-validation.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Credit to https://adventofcode.com/ for this exercise
44
*
55
* Each object in the passwordList gives a password policy and then the password.
6-
* The times field says the minimal and maximal amount of times the letter should be in the password. So 1-3 means at least 1 time, at most 3 times.
6+
* The times field says the minimal and maximal amount of times the letter should be in the password.
7+
* So 1-3 means at least 1 time, at most 3 times.
78
* The letter field gives which letter should be counted
89
* The password field gives the password
910
*
@@ -20,4 +21,18 @@ const passwordList = [
2021
{ times: '1-3', letter: 'a', password: 'abcde'},
2122
{ times: '1-3', letter: 'b', password: 'cdefg'},
2223
{ times: '2-9', letter: 'c', password: 'ccccccccc'}
23-
];
24+
];
25+
26+
passwordList.forEach(passwordPolicy =>{
27+
let count = 0;
28+
for(let i=0; i<passwordPolicy.password.length; i++){
29+
if(passwordPolicy.password[i] === passwordPolicy.letter) {
30+
count +=1;
31+
}
32+
}
33+
if(count >= parseInt(passwordPolicy.times[0]) && count <= parseInt(passwordPolicy.times[2])){
34+
console.log(`${passwordPolicy.password} is VALID, ${passwordPolicy.letter} is present ${count} times and should have been present at least ${parseInt(passwordPolicy.times[0])} and at most ${parseInt(passwordPolicy.times[2])} times`);
35+
} else{
36+
console.log(`${passwordPolicy.password} is INVALID, ${passwordPolicy.letter} is present ${count} times and should have been present at least ${parseInt(passwordPolicy.times[0])} and at most ${parseInt(passwordPolicy.times[2])} times`);
37+
}
38+
})

Week3/challenges/4-bank-account.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,28 @@ const bankAccount = {
2727
],
2828
};
2929

30+
const updateBankAccount = (amount, reason) => {
31+
bankAccount.currentBalance = bankAccount.currentBalance - amount;
32+
bankAccount.transactions.push({prevAmount: bankAccount.currentBalance + amount,
33+
newAmount: bankAccount.currentBalance,
34+
reason: reason});
35+
};
36+
3037
const donateMoney = (amount, onSuccess, onFail) => {
31-
// TODO complete this function
38+
if(bankAccount.currentBalance>amount){
39+
onSuccess();
40+
updateBankAccount(amount, reason = 'Donation');
41+
} else{
42+
onFail();
43+
}
3244
};
3345
const payRent = (amount, onSuccess, onFail) => {
34-
// TODO complete this function
46+
if(bankAccount.currentBalance>amount){
47+
onSuccess();
48+
updateBankAccount(amount, reason = 'Rent');
49+
} else{
50+
onFail();
51+
}
3552
};
3653

3754
/**

0 commit comments

Comments
 (0)