-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetstore.js
115 lines (104 loc) · 3.59 KB
/
petstore.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
/**
* This function should calculate the total amount of pet food that should be
* ordered for the upcoming week.
* @param numAnimals the number of animals in the store
* @param avgFood the average amount of food (in kilograms) eaten by the animals
* each week
* @return the total amount of pet food that should be ordered for the upcoming
* week, or -1 if the numAnimals or avgFood are less than 0 or non-numeric
*/
function calculateFoodOrder(numAnimals, avgFood) {
// IMPLEMENT THIS FUNCTION!
if isNan(numAnimals) == false;
return "-1";
if isNan(avgFood) == false;
return "-1";
if (numAnimals=>0 || avgFood=>0){
return numAnimals*avgFood;
}
/**
* Determines which day of the week had the most nnumber of people visiting the
* pet store. If more than one day of the week has the same, highest amount of
* traffic, an array containing the days (in any order) should be returned.
* (ex. ["Wednesday", "Thursday"]). If the input is null or an empty array, the function
* should return null.
* @param week an array of Weekday objects
* @return a string containing the name of the most popular day of the week if there is only one most popular day, and an array of the strings containing the names of the most popular days if there are more than one that are most popular
*/
function mostPopularDays(week) {
if ((week == null) || (week.length==0)) return null;
var maxTraffic = week[0].traffic;
for (var day of week) {
if (day.traffic > maxTraffic) {
maxTraffic = day.traffic;
}
}
var dayArr = [];
var i=0;
for (var day of week) {
if (day.traffic == maxTraffic) {
dayArr[i++] = day.name;
}
}
if (dayArr.length==1) return dayArr[0];
return dayArr;
}
/**
* Given three arrays of equal length containing information about a list of
* animals - where names[i], types[i], and breeds[i] all relate to a single
* animal - return an array of Animal objects constructed from the provided
* info.
* @param names the array of animal names
* @param types the array of animal types (ex. "Dog", "Cat", "Bird")
* @param breeds the array of animal breeds
* @return an array of Animal objects containing the animals' information, or an
* empty array if the array's lengths are unequal or zero, or if any array is null.
*/
function createAnimalObjects(names, types, breeds) {
if ((names==null) || (types==null) || (breeds==null)) return [];
else if ((names.length==types.length) && (names.length==breeds.length) && (names.length!=0)) {
var arrAnimal = [];
for (var i=0; i<names.length; i++) {
arrAnimal[i] = new Animal(names[i], types[i], breeds[i]);
}
return arrAnimal;
} else {
return [];
}
}
/////////////////////////////////////////////////////////////////
//
// Do not change any code below here!
//
/////////////////////////////////////////////////////////////////
/**
* A prototype to create Weekday objects
*/
function Weekday (name, traffic) {
this.name = name;
this.traffic = traffic;
}
/**
* A prototype to create Item objects
*/
function Item (name, barcode, sellingPrice, buyingPrice) {
this.name = name;
this.barcode = barcode;
this.sellingPrice = sellingPrice;
this.buyingPrice = buyingPrice;
}
/**
* A prototype to create Animal objects
*/
function Animal (name, type, breed) {
this.name = name;
this.type = type;
this.breed = breed;
}
/**
* Use this function to test whether you are able to run JavaScript
* from your browser's console.
*/
function helloworld() {
return 'hello world!';
}