-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathW3D6.js
98 lines (85 loc) · 2.15 KB
/
W3D6.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
/*
Exercise 1
Currently, you have a budget of $100. You have a shopping list in decreasing priority,
and you want to report how many items you bought and how much it cost you.
Write a function called shoppingSummary that takes an array of shopping items (objects),
and returns a string with the number of items you bought, and the total amount you spent.
Notes:
You cannot spend more than your budget ($100).
You have enough room for all the items (if you end up buying everything),
so there's no item limit -- your only limit is your budget.
The list is mentioned to be in "decreasing priority" simply because you do not have to sort the input array to optimize for anything else.
So do not worry about coming up with any other sorting algorithm for the most "bang for your buck" or what not :-)
Take for example the data below:
var shoppingList = [
{
item: "rice",
price: 12.75,
weightInPounds: 20
},
{
item: "chicken",
price: 6.99,
weightInPounds: 1.25
},
{
item: "cookware",
price: 79.99,
weightInPounds: 35
},
{
item: "celery",
price: 3.99,
weightInPounds: 2
},
{
item: "carrots",
price: 2.85,
weightInPounds: 2
},
{
item: "green beans",
price: 2.55,
weightInPounds: 2
}
];
Calling your function should result in:
shoppingSummary(shoppingList); //"I got 3 items at $99.73"
/*
Exercise 2
Suppose that you wanted to take out the most expensive item on your shopping list.
Write a function called removeMostExpensive
which returns a new array without the most expensive thing in the list.
Your function should preserve the order of the items in this array.
removeMostExpensive(shoppingList);
Would return a new array with the following elements:
[
{
item: "rice",
price: 12.75,
weightInPounds: 20
},
{
item: "chicken",
price: 6.99,
weightInPounds: 1.25
},
{
item: "celery",
price: 3.99,
weightInPounds: 2
},
{
item: "carrots",
price: 2.85,
weightInPounds: 2
},
{
item: "green beans",
price: 2.55,
weightInPounds: 2
}
];
//notice that the element with "cookware" is missing
/*
// your answer is here