Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function club(input) {
- let neededSum = Number(input[0]);
- let profit = 0;
- for (let i = 1; i < (input.length || profit >= neededSum); i++) {
- let drink = input[i];
- if (drink === "Party!") {
- break;
- }
- let sum = drink.length * Number(input[++i]);
- if (sum % 2 === 1) {
- sum *= 0.75;
- }
- profit += sum;
- }
- if (neededSum > profit) {
- console.log(`We need ${(neededSum - profit).toFixed(2)} leva more.`);
- } else {
- console.log("Target acquired.");
- }
- console.log(`Club income - ${profit.toFixed(2)} leva.`);
- }
- РЕШЕНИЕ С WHILE И SHIFT():
- function club(input) {
- let neededSum = Number(input.shift());
- let drink = input.shift();
- let profit = 0;
- while (drink !== "Party!" && profit < neededSum) {
- let sum = drink.length * Number(input.shift());
- if (sum % 2 === 1) {
- sum *= 0.75;
- }
- profit += sum;
- drink = input.shift();
- }
- if (neededSum > profit) {
- console.log(`We need ${(neededSum - profit).toFixed(2)} leva more.`);
- } else {
- console.log("Target acquired.");
- }
- console.log(`Club income - ${profit.toFixed(2)} leva.`);
- }
- И С ТЕРНАРЕН ОПЕРАТОР:
- function club(input) {
- let neededSum = Number(input.shift());
- let drink = input.shift();
- let profit = 0;
- while (drink !== "Party!" && profit < neededSum) {
- let sum = drink.length * Number(input.shift());
- sum *= sum % 2 === 1 ? 0.75 : 1;
- profit += sum;
- drink = input.shift();
- }
- console.log(neededSum <= profit ? "Target acquired." :
- `We need ${(neededSum - profit).toFixed(2)} leva more.`);
- console.log(`Club income - ${profit.toFixed(2)} leva.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement