-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge.js
103 lines (87 loc) · 3.16 KB
/
challenge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const checkDogs = function (dogsJulia, dogsKate) {
const julia = dogsJulia.slice(1, 3);
const dogs = julia.concat(dogsKate);
// console.log(dogs);
dogs.forEach(function (dog, i) {
if (dog >= 3) {
console.log(`Dog number ${i + 1}
is an adult, and is ${dog} years old`);
} else {
console.log(`Dog number 2 is still a puppy 🐶`);
}
});
};
// checkDogs([3, 5, 2, 12, 7], [4, 1, 15, 8, 3]);
// checkDogs([9, 16, 6, 8, 3], [10, 5, 6, 1, 4]);
//////////////////////////////////////////////challenge no.2/////////////////////////////////////////////////////////
// const age = [3, 5, 2, 12, 7];
// const calcAverageHumanAge = function (ages) {
// const humanAges = ages.map(age => (age <= 2 ? 2 * age : 16 + age * 4));
// const adults = humanAges.filter(age => age >= 18);
// console.log(humanAges);
// console.log(adults);
// const averageHumanAge =
// adults.reduce((acc, age) => acc + age) / adults.length;
// return averageHumanAge;
// console.log(averageHumanAge);
// };
// const calcAverageHumanAge = function (ages) {
// const humanAges = ages
// .map(age => (age <= 2 ? 2 * age : 16 + age * 4))
// .filter(age => age >= 18)
// .reduce((acc, age, i, arr) => acc + age / arr.length, 0);
// console.log(humanAges);
// };
// const calcAverageHumanAge = ages =>
// ages
// .map(age => (age <= 2 ? 2 * age : 16 + age * 4))
// .filter(age => age >= 18)
// .reduce((acc, age, i, arr) => acc + age / arr.length, 0);
// // console.log(humanAges);
// const avg1 = calcAverageHumanAge([5, 2, 4, 1, 15, 8, 3]);
// const avg2 = calcAverageHumanAge([16, 6, 10, 5, 6, 1, 4]);
// console.log(avg1, avg2);
// calcAverageHumanAge([5, 2, 4, 1, 15, 8, 3]);
// calcAverageHumanAge([16, 6, 10, 5, 6, 1, 4]);
/////////////////////////////////////////challenge no.3/////////////////////////////////////////////
const dogs = [
{ weight: 22, curFood: 250, owners: ['Alice', 'Bob'] },
{ weight: 8, curFood: 200, owners: ['Matilda'] },
{ weight: 13, curFood: 275, owners: ['Sarah', 'John'] },
{ weight: 32, curFood: 340, owners: ['Michael'] },
];
//1.
dogs.forEach(dog => (dog.recFood = Math.trunc(dog.weight ** 0.75 * 28)));
// console.log(dogs);
//2.
const dogSarah = dogs.find(dog => dog.owners.includes('Sarah'));
console.log(dogSarah);
console.log(
`Sarah's dog is eating too ${
dogSarah.curFood > dogSarah.recFood ? 'much' : 'little'
}`
);
//3.
const ownersEatTooMuch = dogs
.filter(dog => dog.curFood > dog.recFood)
.flatMap(dog => dog.owners);
const ownersEatTooLittle = dogs
.filter(dog => dog.curFood < dog.recFood)
.flatMap(dog => dog.owners);
console.log(ownersEatTooLittle);
console.log(ownersEatTooMuch);
//4.
console.log(`${ownersEatTooMuch.join(' and ')}'s dogs eat too much!`);
console.log(`${ownersEatTooLittle.join(' and ')}'s dogs eat too little!`);
//5.
console.log(dogs.some(dog => dog.curFood === dog.recFood));
//6.
const checkEatingOk = dog =>
dog.curFood > dog.recFood * 0.9 && dog.curFood < dog.recFood * 1.1;
console.log(dogs.some(checkEatingOk));
//7.
console.log(dogs.filter(checkEatingOk));
//8.
const dogsSorted = dogs.slice().sort((a, b) => a.recFood - b.recFood);
console.log(dogsSorted);