Advertisement
Lulunga

mid exam 02. Hello, France

Jun 29th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let itemsCollection = input[0].split('|');
  3.     let budget = Number(input[1]);
  4.     let validPrices = [];
  5.     for (let line of itemsCollection) {
  6.         let [type, price] = line.split('->');
  7.         price = Number(price);
  8.         let isValid = validPrice(type, price);
  9.         if (isValid && price <= budget) {
  10.             budget -= price;
  11.             validPrices.push(price);
  12.         }
  13.     }
  14.     validPrices = validPrices.map(Number);
  15.     let expenses = validPrices.reduce(function (accumulator, currentValue) {
  16.         return accumulator + currentValue;
  17.     });
  18.     let profit = 0.4 * expenses;
  19.     let newPrices = validPrices.slice(0).map(el => el * 1.4);
  20.     budget = budget + newPrices.reduce(function (accumulator, currentValue) {
  21.         return accumulator + currentValue;
  22.     });
  23.     newPrices = newPrices.map(el => el.toFixed(2));
  24.     console.log(newPrices.join(' '));
  25.  
  26.     console.log(`Profit: ${profit.toFixed(2)}`);
  27.     budget >= 150
  28.         ? console.log("Hello, France!")
  29.         : console.log("Time to go.");
  30.  
  31.  
  32.     function validPrice(type, price) {
  33.         if (type === 'Clothes' && price <= 50 && price > 0) {
  34.             return true;
  35.         } else if (type === 'Shoes' && price <= 35 && price > 0) {
  36.             return true;
  37.         } else if (type === 'Accessories' && price <= 20.50 && price > 0) {
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement