Skip to content

Commit 9b9ec58

Browse files
committed
Challenges for week3 completed.
1 parent cea9afc commit 9b9ec58

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎Week3/challenges/1-sum-entries.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ 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
12+
const addResult = 2020;
13+
14+
for(let ii = 0; ii < list.length; ii++){
15+
for(let jj = ii + 1; jj < list.length; jj++){
16+
if(list[ii] + list[jj] === addResult) {
17+
result = list[ii] * list[jj];
18+
// break statement here
19+
}
20+
}
21+
// break statement here
22+
}
1423

1524

1625
// TEST CODE, do not change

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ 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
12+
const addUpResult = 2020;
1413

14+
for(let ii = 0; ii < list.length; ii++){
15+
for(let jj = ii + 1; jj < list.length; jj++){
16+
for (let kk = ii + 1; kk < list.length; kk++){
17+
if (list[ii] + list[jj] + list[kk] === addUpResult){
18+
result = list[ii] * list[jj] * list[kk];
19+
// break statement here
20+
}
21+
}
22+
// break statement here
23+
}
24+
// break statement here
25+
}
1526

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

‎Week3/challenges/3-password-validation.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,24 @@ const passwordList = [
2020
{ times: '1-3', letter: 'a', password: 'abcde'},
2121
{ times: '1-3', letter: 'b', password: 'cdefg'},
2222
{ times: '2-9', letter: 'c', password: 'ccccccccc'}
23-
];
23+
];
24+
25+
const splitStr = input => input.split('-').map(char => Number(char));
26+
27+
const validatePassword = passwordList => {
28+
passwordList.forEach(({times, letter, password}) => {
29+
30+
const passwordChar = password.split('');
31+
let letterCounts = passwordChar.filter(character => { return character === letter; }).length;
32+
33+
const result = splitStr(times);
34+
if(letterCounts < result[0] || letterCounts > result[1]){
35+
console.log(`'${password}' is INVALID ${letter} is present ${letterCounts} times and should have been present at least ${result[0]} and at most ${result[1]} times`);
36+
}else{
37+
console.log(`'${password}' is VALID ${letter} is present ${letterCounts} times and should have been present at least ${result[0]} and at most ${result[1]} times`);
38+
}
39+
});
40+
};
41+
42+
validatePassword(passwordList)
43+

‎Week3/challenges/4-bank-account.js‎

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

30+
const createTransaction = (amount, onSuccess, onFail, reason) => {
31+
if (amount > bankAccount.currentBalance) {
32+
onFail();
33+
return 0;
34+
}
35+
36+
bankAccount.transactions.push({
37+
prevAmount: bankAccount.currentBalance,
38+
newAmount: bankAccount.currentBalance - amount,
39+
reason: reason
40+
});
41+
bankAccount.currentBalance -= amount;
42+
onSuccess();
43+
};
44+
3045
const donateMoney = (amount, onSuccess, onFail) => {
31-
// TODO complete this function
46+
createTransaction(amount, onSuccess, onFail, "Donation");
3247
};
3348
const payRent = (amount, onSuccess, onFail) => {
34-
// TODO complete this function
49+
createTransaction(amount, onSuccess, onFail, "Rent");
3550
};
3651

3752
/**

0 commit comments

Comments
 (0)