Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ С FOR:
- function dishwasher(input) {
- let soupMl = Number(input[0]) * 750;
- let dishes = 0;
- let pots = 0;
- for (let i = 1; i < input.length; i++) {
- let command = input[i];
- if (command === 'End') {
- console.log('Detergent was enough!');
- console.log(`${dishes} dishes and ${pots} pots were washed.`);
- console.log(`Leftover detergent ${soupMl} ml.`);
- break;
- }
- let charge = Number(command);
- if (i % 3 !== 0) {
- dishes += charge;
- soupMl -= charge * 5;
- } else {
- pots += charge;
- soupMl -= charge * 15;
- }
- if (soupMl < 0) {
- console.log(`Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!`);
- break;
- }
- }
- }
- РЕШЕНИЕ С WHILE:
- function dishwasher(input) {
- let soupMl = Number(input[0]) * 750;
- let dishes = 0;
- let pots = 0;
- let counter = 1;
- let command;
- while ((command = input[counter++]) !== 'End') {
- let charge = Number(command);
- if (counter % 3 !== 1) {
- dishes += charge;
- soupMl -= charge * 5;
- } else {
- pots += charge;
- soupMl -= charge * 15;
- }
- if (soupMl < 0) {
- console.log(`Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!`);
- return;
- }
- }
- console.log('Detergent was enough!');
- console.log(`${dishes} dishes and ${pots} pots were washed.`);
- console.log(`Leftover detergent ${soupMl} ml.`);
- }
- РЕШЕНИЕ С FOR И ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:
- function dishwasher(input) {
- let soupMl = Number(input[0]) * 750;
- let dishes = 0;
- let pots = 0;
- for (let i = 1; i < input.length && soupMl >= 0 && input[i] !== 'End'; i++) {
- i % 3 !== 0 ? dishes += Number(input[i]) : pots += Number(input[i]);
- soupMl -= Number(input[i]) * (i % 3 !== 0 ? 5 : 15);
- }
- console.log(soupMl < 0 ? `Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!` :
- `Detergent was enough!\n${dishes} dishes and ${pots} pots were washed.\nLeftover detergent ${soupMl} ml.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement