Repository navigation
Expand file tree
/
Copy pathisAdmissibleOverpayment.js
More file actions
66 lines (54 loc) · 2.3 KB
/
Copy pathisAdmissibleOverpayment.js
File metadata and controls
66 lines (54 loc) · 2.3 KB
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
/*
After recently joining Instacart's beta testing developer group, you decide to
experiment with their new API. You know that the API returns item-specific
display-ready strings like 10.0% higher than in-store or 5.0% lower than
in-store that inform users when the price of an item is different from the
one in-store. But you want to extend this functionality by giving people a
better sense of how much more they will be paying for their entire shopping cart.
Your app lets a user decide the total amount x they are willing to pay via
Instacart over in-store prices. This you call their price sensitivity.
Your job is to determine whether a given customer will be willing to pay for
the given items in their cart based on their stated price sensitivity x.
Example
For
prices = [110, 95, 70],
notes = ["10.0% higher than in-store",
"5.0% lower than in-store",
"Same as in-store"]
and x = 5, the output should be
isAdmissibleOverpayment(prices, notes, x) = true.
In-store prices of the first and the second items are 100, and the price of
the third item is 70, which means the customer is overpaying 10 - 5 + 0 = 5,
which they are willing to do based on their price sensitivity.
*/
function isAdmissibleOverpayment(prices, notes, x) {
let percents = [];
for (let i = 0; i < notes.length; i++) {
let splited = notes[i].split('%');
percents.push(+splited[0]);
}
let count = [];
for (let i = 0; i < prices.length; i++) {
let diff = ((prices[i] * 100) / 100 + percents[i]) - prices[i];
if (!(isNaN(diff))) {
count.push(diff);
};
};
let result = count.reduce((a,b) => a-b);
return result <= x ? true : false;
};
console.log(isAdmissibleOverpayment([110, 95, 70],
["10.0% higher than in-store",
"5.0% lower than in-store",
"Same as in-store"],
5)) //true
console.log(isAdmissibleOverpayment([48, 165],
["20.00% lower than in-store",
"10.00% higher than in-store"],
3)) //false
console.log(isAdmissibleOverpayment([40, 40, 40, 40],
["0.001% higher than in-store",
"0.0% lower than in-store",
"0.0% higher than in-store",
"0.0% lower than in-store"],
0)) //false