Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function touristShop(input) {
- let i = 0;
- let sum = 0;
- let counter = 0;
- let budget = Number(input[i++]);
- let command = input[i++];
- while (command !== "Stop" && sum <= budget) {
- let price = Number(input[i++]);
- counter++;
- if (counter % 3 === 0) {
- price /= 2;
- }
- sum += price;
- command = input[i++];
- }
- if (command === "Stop") {
- console.log(`You bought ${counter} products for ${sum.toFixed(2)} leva.`);
- } else {
- console.log("You don't have enough money!")
- console.log(`You need ${(sum - budget).toFixed(2)} leva!`)
- }
- }
- РЕШЕНИЕ СЪС SHIFT():
- function touristShop(input) {
- let sum = 0;
- let counter = 0;
- let budget = Number(input.shift());
- let command = input.shift();
- while (command !== "Stop" && sum <= budget) {
- let price = Number(input.shift());
- counter++;
- if (counter % 3 === 0) {
- price /= 2;
- }
- sum += price;
- command = input.shift();
- }
- if (command === "Stop") {
- console.log(`You bought ${counter} products for ${sum.toFixed(2)} leva.`);
- } else {
- console.log("You don't have enough money!")
- console.log(`You need ${(sum - budget).toFixed(2)} leva!`)
- }
- }
- РЕШЕНИЕ С FOR:
- function touristShop(input) {
- let sum = 0;
- let budget = Number(input[0]);
- for (let i = 1; i < input.length; i++) {
- let command = input[i];
- if (command === "Stop") {
- console.log(`You bought ${parseInt(i / 2)} products for ${sum.toFixed(2)} leva.`);
- break;
- }
- let priceProduct = Number(input[++i]);
- if (i % 3 === 0) {
- priceProduct /= 2;
- }
- sum += priceProduct;
- if (sum > budget) {
- console.log("You don't have enough money!");
- console.log(`You need ${(sum - budget).toFixed(2)} leva!`);
- break;
- }
- }
- }
- И ЛЕКО ТАРИКАТСКАТА СЪС SHIFT() И ТЕРНАРЕН ОПЕРАТОР:
- function touristShop(input) {
- let sum = 0;
- let budget = Number(input.shift());
- for (let i = 1; i < Number.MAX_SAFE_INTEGER; i++) {
- if (input.shift() === "Stop") {
- console.log(`You bought ${--i} products for ${sum.toFixed(2)} leva.`);
- break;
- }
- sum += Number(input.shift()) * (i % 3 === 0 ? 0.5 : 1);
- if (sum > budget) {
- console.log(`You don't have enough money!\nYou need ${(sum - budget).toFixed(2)} leva!`);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement