Coding Challenge #4
Julia and Kate are still studying dogs, and this time they are studying if dogs are eating too much or too little.
Eating too much means the dog's current food portion is larger than the recommended portion, and eating too little is the opposite.
Eating an okay amount means the dog's current food portion is within a range 10% above and 10% below the recommended portion (see hint).
Your tasks
- Loop over the 'dogs' array containing dog objects, and for each dog, calculate the recommended food portion and add it to the object as a new property. Do not create a new array, simply loop over the array. Formula:
recommendedFood = weight ** 0.75 * 28
. (The result is in grams of food, and the weight needs to be in kg)
- Find Sarah's dog and log to the console whether it's eating too much or too little. Hint: Some dogs have multiple owners, so you first need to find Sarah in the owners array, and so this one is a bit tricky (on purpose) 🤓
- Create an array containing all owners of dogs who eat too much ('ownersEatTooMuch') and an array with all owners of dogs who eat too little ('ownersEatTooLittle').
- Log a string to the console for each array created in 3., like this: "Matilda and Alice and Bob's dogs eat too much!" and "Sarah and John and Michael's dogs eat too little!"
- Log to the console whether there is any dog eating exactly the amount of food that is recommended (just
true
or false
)
- Log to the console whether there is any dog eating an okay amount of food (just
true
or false
)
- Create an array containing the dogs that are eating an okay amount of food (try to reuse the condition used in 6.)
- Create a shallow copy of the 'dogs' array and sort it by recommended food portion in an ascending order (keep in mind that the portions are inside the array's objects 😉)
Hints
- Use many different tools to solve these challenges, you can use the summary lecture to choose between them 😉
- Being within a range 10% above and below the recommended portion means:
current > (recommended * 0.90) && current < (recommended * 1.10)
. Basically, the current portion should be between 90% and 110% of the recommended portion.
Test data
| 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'] },
];
|
GOOD LUCK 😀
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 | // 1. Loop over the 'dogs' array containing dog objects, and for each dog, calculate the recommended food portion and add it to the object as a new property. Do not create a new array, simply loop over the array. Forumla: recommendedFood = weight ** 0.75 * 28. (The result is in grams of food, and the weight needs to be in kg)
dogs.forEach((dog) => {
dog.recommendedFood = Math.trunc(dog.weight ** 0.75 * 28);
});
// 2. Find Sarah's dog and log to the console whether it's eating too much or too little. Hint: Some dogs have multiple owners, so you first need to find Sarah in the owners array, and so this one is a bit tricky (on purpose)
// const sarahsDog = dogs.filter((dog) => dog.owners.includes("Sarah"));
const sarahsDog = dogs.find((dog) => dog.owners.includes("Sarah"));
console.log(
`Sarah's dog is eating ${
sarahsDog.curFood > sarahsDog.recommendedFood ? "much" : "little"
}`
);
// filter 返回一个新数组,包含所有满足条件的元素。(可以返回零个或多个元素)
// find 返回第一个满足条件的元素。(只返回第一个符合条件的元素,都不符合就返回 undefined)
// 3. Create an array containing all owners of dogs who eat too much ('ownersEatTooMuch') and an array with all owners of dogs who eat too little ('ownersEatTooLittle').
const ownersEatTooMuch = dogs
.filter((dog) => dog.curFood > dog.recommendedFood)
.map((dog) => dog.owners)
.flat();
const ownersEatTooLittle = dogs
.filter(({ curFood, recommendedFood }) => curFood < recommendedFood)
.flatMap(({ owners }) => owners);
console.log(ownersEatTooMuch);
console.log(ownersEatTooLittle);
// 链式调用 map 和 fiat,可以直接用 flatMap
// 4. Log a string to the console for each array created in 3., like this: "Matilda and Alice and Bob's dogs eat too much!" and "Sarah and John and Michael's dogs eat too little!"
console.log(`${ownersEatTooMuch.join(" and ")}'s dogs eat too much!`);
console.log(`${ownersEatTooLittle.join(" and ")}'s dogs eat too little!`);
// 5. Log to the console whether there is any dog eating exactly the amount of food that is recommended (just true or false)
// console.log(
// `${dogs.find((dog) => dog.curFood === dog.recommendedFood) ? true : false}`
// );
console.log(dogs.some((dog) => dog.curFood === dog.recommendedFood));
// 6. Log to the console whether there is any dog eating an okay amount of food (just true or false)
console.log(
dogs.some(
(dog) =>
dog.curFood > dog.recommendedFood * 0.9 &&
dog.curFood < dog.recommendedFood * 1.1
)
);
// some 返回布尔值
// 7. Create an array containing the dogs that are eating an okay amount of food (try to reuse the condition used in 6.)
console.log(
dogs.filter(
(dog) =>
dog.curFood > dog.recommendedFood * 0.9 &&
dog.curFood < dog.recommendedFood * 1.1
)
);
// 8. Create a shallow copy of the 'dogs' array and sort it by recommended food portion in an ascending order (keep in mind that the portions are inside the array's objects)
const dogsCopy = dogs
.slice()
.sort((a, b) => a.recommendedFood - b.recommendedFood);
console.log(dogsCopy);
// 浅拷贝直接用 slice(),sort() 升序一般都是用 a,b => a-b
|