• API
• FAQ
• Tools
• Archive
daily pastebin goal
72%
SHARE
TWEET

# Untitled

a guest Jan 21st, 2019 77 in 87 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. solve([
2.     '1.00, coffee, caffeine, milk, 4',
3.     '0.40, tea, milk, 2',
4.     '1.00, coffee, decaf, 0'
5. ]);
6.
7. function solve(arr) {
8.
9.     let priceObj =
10.         {
11.             caffeine: 0.80,
12.             decaf: 0.90,
13.             tea: 0.80
14.         }
15.
16.
17.     let dictionary = [];
18.
19.
20.     let totalIncome = 0;
21.
22.     let priceOfOrder = 0;
23.
24.     for (let i = 0; i < arr.length; i++) {
25.         dictionary.push(arr[i].split(', '));
26.
27.         //Current Order
28.         let innerArray = dictionary[i];
29.
30.         //When it has length 5
31.         if (innerArray.length === 5) {
32.             if (innerArray.includes('caffeine')) {
33.               priceOfOrder=  priceObj['caffeine'] + 0.1;
34.
35.             } else if (innerArray.includes('decaf')) {
36.                priceOfOrder= priceObj['decaf'] +0.1;
37.             }
38.         }
39.         //When it has length 4
40.                else if (innerArray.length === 4) {
41.             if (innerArray.includes('caffeine')) {
42.                 priceOfOrder = priceObj['caffeine'];
43.
44.
45.             } else if (innerArray.includes('decaf')) {
46.                 priceOfOrder = priceObj['decaf'];
47.
48.             } else if (innerArray.includes('tea')) {
49.                 priceOfOrder = priceObj['tea'] + 0.1;
50.             }
51.         }
52.         //When it has length 3
53.         else {
54.             priceOfOrder = priceObj['tea'];
55.         }
57.         innerArray[innerArray.length - 1] > 0 ? priceOfOrder += 0.1 : priceOfOrder = priceOfOrder;
58.
59.         //Print Order
60.         if (priceOfOrder > innerArray[0]) {
61.             console.log(`Not enough money for \${innerArray[1]}. Need \${(priceOfOrder - innerArray[0]).toFixed(2)}\$ more.`);
62.             priceOfOrder = 0;
63.         } else {
64.             console.log(`You ordered \${innerArray[1]}. Price: \${priceOfOrder.toFixed(2)}\$ Change: \${(innerArray[0] - priceOfOrder).toFixed(2)}\$`);
65.         }