-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
118 lines (108 loc) · 3.57 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
You may use the following animals array to test your functions inside of this file. This is the same array of animals that is used in the tests.
To run this file, remember that you can run `node index.js` from the command line. You will need to be in the directory where the `index.js` file is kept for the above command to work.
Finally, remember that in order for you to see output on the command line, you must log something out.
*/
const animals = [
{ kind: "Pig", count: 5 },
{ kind: "Cow", count: 7 },
{ kind: "Chicken", count: 11 },
{ kind: "Horse", count: 1 },
{ kind: "Dog", count: 2 },
{ kind: "Cat", count: 2 },
];
/**
* FUNCTION DESCRIPTION
* ------------------
* Adds up the `count` key of each animal in the array and returns a total.
* @param {Object[]} animals - An array of animal objects. See above for the shape of the object.
* @returns {number} The total of all animal counts.
*
* EXAMPLE
* ------------------
* getTotalCount(animals); //> 28
* getTotalCount([]); //> 0 // returns 0 if the input array is empty
*/
function getTotalCount(animals) {
let num = 0;
for (let animal of animals) {
num += animal.count
}
return num;
}
/**
* FUNCTION DESCRIPTION
* ------------------
* Returns the `kind` for each object from the inputted array.
* @param {Object[]} animals - An array of animal objects. See above for the shape of the object.
* @returns {string[]} An array of strings, where each string is a kind of animal.
*
* EXAMPLE
* ------------------
* getAllKinds(animals); //> ["Pig", "Cow", "Chicken", "Horse", "Dog", "Cat"]
* getAllKinds([]); //> [] // returns empty array if input array is empty
*/
function getAllKinds(animals) {
let arr = [];
for (let animal of animals) {
arr.push(animal.kind)
}
return arr;
}
/**
* FUNCTION DESCRIPTION
* ------------------
* Returns all objects inside the given array where the `count` of an animal is equal to or greater than the `minimum` amount.
* @param {Object[]} animals - An array of animal objects. See above for the shape of the object.
* @param {number} minimum - The lowest count of animals to include.
* @returns {Object[]} An array of objects, which represents all animals that have a count equal to or greater than the minimum.
*
* EXAMPLE
* ------------------
* filterByCountMinimum(animals, 5); //> [
{ kind: "Pig", count: 5 },
{ kind: "Cow", count: 7 },
{ kind: "Chicken", count: 11 },
];
* filterByCountMinimum([], 3); //> [] // returns empty array if input array is empty
*/
function filterByCountMinimum(animals, minimum) {
let arr = [];
for (let animal of animals) {
if (animal.count >= minimum) {
arr.push(animal)
}
}
return arr;
}
/**
* FUNCTION DESCRIPTION
* ------------------
* Returns the object with the highest `count`. If more than one object shares the same highest `count`, return the first one.
* @param {Object[]} animals - An array of animal objects. See above for the shape of the object.
* @returns {Object} The object which represents the animal with the highest count.
*
* EXAMPLE
* ------------------
* getMostCommonAnimal(animals); //> { kind: "Chicken", count: 11 }
* getMostCommonAnimal([]); //> null // returns null if the input is empty
*/
function getMostCommonAnimal(animals) {
let result = null;
if (animals[0]) {
result = animals[0]
}
for (let animal of animals) {
if (result.count < animal.count) {
result = animal
}
}
return result;
}
// Do not change anything below this line.
module.exports = {
getTotalCount,
getAllKinds,
filterByCountMinimum,
getMostCommonAnimal,
};