Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let itemsCollection = input[0].split('|');
- let budget = Number(input[1]);
- let validPrices = [];
- for (let line of itemsCollection) {
- let [type, price] = line.split('->');
- price = Number(price);
- let isValid = validPrice(type, price);
- if (isValid && price <= budget) {
- budget -= price;
- validPrices.push(price);
- }
- }
- validPrices = validPrices.map(Number);
- let expenses = validPrices.reduce(function (accumulator, currentValue) {
- return accumulator + currentValue;
- });
- let profit = 0.4 * expenses;
- let newPrices = validPrices.slice(0).map(el => el * 1.4);
- budget = budget + newPrices.reduce(function (accumulator, currentValue) {
- return accumulator + currentValue;
- });
- newPrices = newPrices.map(el => el.toFixed(2));
- console.log(newPrices.join(' '));
- console.log(`Profit: ${profit.toFixed(2)}`);
- budget >= 150
- ? console.log("Hello, France!")
- : console.log("Time to go.");
- function validPrice(type, price) {
- if (type === 'Clothes' && price <= 50 && price > 0) {
- return true;
- } else if (type === 'Shoes' && price <= 35 && price > 0) {
- return true;
- } else if (type === 'Accessories' && price <= 20.50 && price > 0) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement