Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function careOfPuppy(input) {
- let food = Number(input[0]) * 1000;
- let i = 1;
- let command = input[i++]
- while (command !== "Adopted") {
- food -= Number(command);
- command = input[i++];
- }
- if (food >= 0) {
- console.log(`Food is enough! Leftovers: ${food} grams.`)
- } else {
- console.log(`Food is not enough. You need ${Math.abs(food)} grams more.`);
- }
- }
- РЕШЕНИЕ С FOR:
- function careOfPuppy(input) {
- let food = Number(input[0]) * 1000;
- let command;
- for (let i = 1; i < input.length; i++) {
- command = input[i];
- if (command === "Adopted") {
- break;
- }
- food -= Number(command);
- }
- if (food >= 0) {
- console.log(`Food is enough! Leftovers: ${food} grams.`)
- } else {
- console.log(`Food is not enough. You need ${Math.abs(food)} grams more.`);
- }
- }
- РЕШЕНИЕ СЪС SHIFT() И ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:)
- function careOfPuppy(input) {
- let food = Number(input.shift()) * 1000;
- while (input[0] !== "Adopted") {
- food -= Number(input.shift());
- }
- console.log(food >= 0 ? `Food is enough! Leftovers: ${food} grams.`
- : `Food is not enough. You need ${Math.abs(food)} grams more.`);
- }
- РЕШЕНИЕ БЕЗ ЦИКЪЛ:
- function careOfPuppy(input) {
- let food = Number(input.shift()) * 1000 - input.slice(0, -1).map(Number).reduce((a, b) => a + b, 0);
- console.log(food >= 0 ? `Food is enough! Leftovers: ${food} grams.` : `Food is not enough. You need ${Math.abs(food)} grams more.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement