Skip to content

Commit 328edad

Browse files
committed
complete array method practice
1 parent 1f86c82 commit 328edad

File tree

3 files changed

+171
-2
lines changed

3 files changed

+171
-2
lines changed

11_Working_with_Arrays/challenge.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
'use-strict';
12
/*
2-
------------Coding Challenge 1------------
3+
? ------------Coding Challenge 1------------
34
45
Julia and Kate are doing a study on dogs. So each of them asked 5 dog owners about their dog's age, and stored the data into an array (one array for each). For now, they are just interested in knowing wether a dog is an adult or a puppy. A dog is an adult if it is at least 3 years old, and it's a puppy if it's less than 3 years old.
56
@@ -42,3 +43,59 @@ const checkDogs = function (dogJulia, dogKate) {
4243
checkDogs([3, 5, 2, 12, 17], [4, 1, 15, 8, 3]);
4344
console.log('TEST DATA 2');
4445
checkDogs([9, 16, 6, 8, 3], [10, 5, 6, 1, 4]);
46+
47+
/*
48+
? ------------Coding Challenge 2------------
49+
50+
Let's go back to Julia and Kate's study about dogs. This time, they want to convert dog ages to human ages and calculate the average age of the dogs in their study.
51+
52+
Create a function `calcAverageHumanAge`, which accepts an array of dog's ages, and does the following things:
53+
54+
1. Calculate the dog age in human years using the following formula: if the dog is <= 2 years old, humanAge = 2 * dogAge. If the dog is > 2 years old, humanAge = 16 + dogAge * 4.
55+
56+
2. Exclude all dogs that are less than 18 human years old (which is the same as keeping dogs that are at least 18 years old)
57+
58+
3. Calculate the average human age of all adult dogs (you should already know from other challenges how we calculate averages 🙂)
59+
60+
4. Run the function for both test datasets
61+
62+
TEST DATA 1: [5, 2, 4, 1, 15, 8, 3],
63+
TEST DATA 2: [16, 6, 10, 5, 6, 1, 4],
64+
65+
GOOD LUCK 🙂
66+
*/
67+
68+
// function calcAverageHumanAge(ages) {
69+
// const humanAge = ages
70+
// .map(age => {
71+
// if (age <= 2) {
72+
// return age * 2;
73+
// } else {
74+
// return 16 + age * 4;
75+
// }
76+
// })
77+
// .filter(dogs => dogs >= 18)
78+
// .reduce((accumulator, currentEl, index, array) => {
79+
// return accumulator + currentEl / array.length);
80+
// }, 0);
81+
// return humanAge;
82+
// }
83+
84+
function calcAverageHumanAge(ages) {
85+
const humanAge = ages.map(age => {
86+
if (age <= 2) {
87+
return age * 2;
88+
} else {
89+
return 16 + age * 4;
90+
}
91+
});
92+
const adultDogs = humanAge.filter(dogs => dogs >= 18);
93+
console.log(adultDogs);
94+
95+
const avr = adultDogs.reduce((accumulator, currentEl, index, array) => {
96+
return accumulator + currentEl / array.length;
97+
}, 0);
98+
return avr;
99+
}
100+
console.log(calcAverageHumanAge([5, 2, 4, 1, 15, 8, 3]));
101+
console.log(calcAverageHumanAge([16, 6, 10, 5, 6, 1, 4]));

11_Working_with_Arrays/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,6 @@ <h2>Close account</h2>
131131
&copy; by Jonas Schmedtmann. Don't claim as your own :)
132132
</footer> -->
133133

134-
<script src="script.js"></script>
134+
<script src="challenge.js"></script>
135135
</body>
136136
</html>

11_Working_with_Arrays/script.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,115 @@ console.log(max);
263263
// )
264264
// : console.log(`Transaction ${i}: You deposited ${transaction}`, array);
265265
// });
266+
267+
const num = movements.find(mov => mov > 3000);
268+
console.log(movements);
269+
console.log(num);
270+
271+
console.log(accounts);
272+
const account = accounts.find(acc => acc.owner === 'Steven Thomas Williams');
273+
console.log(account.owner);
274+
275+
for (const val of accounts) {
276+
if (val.owner === 'Jessica Davis') {
277+
console.log(val);
278+
}
279+
}
280+
281+
btnClose.addEventListener('click', function (e) {
282+
e.preventDefault();
283+
console.log('Delete');
284+
});
285+
286+
// Strings
287+
const names = ['John', 'Zach', 'Adam', 'Martha'];
288+
console.log(names.sort());
289+
290+
console.log(movements);
291+
292+
// return < 0, A, B
293+
// return > 0, B, A
294+
movements.sort((a, b) => a - b);
295+
movements.sort((a, b) => b - a);
296+
console.log(movements);
297+
298+
const arr = [1, 2, 3, 4, 5, 6, 7];
299+
const x = new Array(7);
300+
console.log(x);
301+
// console.log(x.map(() => 5));
302+
x.fill(1, 3);
303+
x.fill(1);
304+
arr.fill(23, 0, 6);
305+
console.log(arr);
306+
console.log(x);
307+
308+
// Array.from
309+
const y = Array.from({ length: 7 }, () => 1);
310+
console.log(y);
311+
312+
const z = Array.from({ length: 7 }, (_curr, i) => i + 1);
313+
console.log(z);
314+
315+
const randomDiceRoll = Array.from({ length: 100 }, (curr, i) => {
316+
return Math.floor(Math.random(i) * 100 + 1);
317+
});
318+
console.log(randomDiceRoll);
319+
320+
// Array method practice
321+
// Exercise 1.
322+
const bankDepositSum = accounts
323+
.flatMap(acc => acc.movements)
324+
.filter(acc => acc > 0)
325+
.reduce((sum, cur) => sum + cur, 0);
326+
console.log(bankDepositSum);
327+
328+
// 2. Get values at least 1000
329+
// const numDeposits1000 = accounts
330+
// .flatMap(acc => acc.movements)
331+
// .filter(mov => mov >= 1000).length;
332+
// console.log(numDeposits1000);
333+
334+
// 2. Get values at least 1000
335+
const numDeposits1000 = accounts
336+
.flatMap(acc => acc.movements)
337+
// .reduce((acc, i) => (i >= 1000 ? acc + 1 : acc), 0);
338+
.reduce((acc, currEl) => (currEl >= 1000 ? acc + 1 : acc), 0);
339+
console.log(numDeposits1000);
340+
341+
// Prefixed ++ operator
342+
let a = 10;
343+
console.log(++a);
344+
console.log(a++);
345+
346+
// 3.
347+
const { depo, withd } = accounts
348+
.flatMap(acc => acc.movements)
349+
.reduce(
350+
(acc, currEl) => {
351+
// currEl > 0 ? (acc.deposits += currEl) : (acc.withdrawals += currEl);
352+
acc[currEl > 0 ? 'deposits' : 'withdrawals'] += currEl;
353+
return acc;
354+
},
355+
{ deposits: 0, withdrawals: 0 }
356+
);
357+
console.log(depo, withd);
358+
359+
// 4.
360+
// this is a nice title > This Is a Nice Title.
361+
const convertTitleCase = function (title) {
362+
const capitalize = str => str[0].toUpperCase() + str.slice(1);
363+
const exceptions = ['a', 'an', 'and', 'the', 'but', 'or', 'on', 'in', 'with'];
364+
365+
const titleCase = title
366+
.toLowerCase()
367+
.split(' ')
368+
.map(word =>
369+
exceptions.includes(word) ? word : word[0].toUpperCase() + word.slice(1)
370+
)
371+
.join(' ');
372+
return capitalize(titleCase);
373+
};
374+
375+
console.log(convertTitleCase('this is a nice title'));
376+
console.log(convertTitleCase('this is a LONG title but not too long'));
377+
console.log(convertTitleCase('and here is another title with an EXAMPLE'));

0 commit comments

Comments
 (0)