Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function christmasGifts(input) {
- let adults = 0;
- let kids = 0;
- for (let i = 0; i < input.length; i++) {
- let command = input[i];
- if (command === "Christmas") {
- break;
- }
- let years = Number(command);
- if (years <= 16) {
- kids++;
- } else {
- adults++;
- }
- }
- let kidsPrice = kids * 5;
- let adultsPrice = adults * 15;
- console.log(`Number of adults: ${adults}`);
- console.log(`Number of kids: ${kids}`);
- console.log(`Money for toys: ${kidsPrice}`);
- console.log(`Money for sweaters: ${adultsPrice}`);
- }
- РЕШЕНИЯ С WHILE:
- function christmasGifts(input) {
- let command = input[0];
- let index = 1;
- let adults = 0;
- let kids = 0;
- while (command !== "Christmas") {
- let years = Number(command);
- if (years <= 16) {
- kids++;
- } else {
- adults++;
- }
- command = input[index++];
- }
- let kidsPrice = kids * 5;
- let adultsPrice = adults * 15;
- console.log(`Number of adults: ${adults}`);
- console.log(`Number of kids: ${kids}`);
- console.log(`Money for toys: ${kidsPrice}`);
- console.log(`Money for sweaters: ${adultsPrice}`);
- }
- Решение с тернарен оператор и леко тарикатската:)
- function christmasGifts(input) {
- let command;
- let adults = 0;
- let kids = 0;
- while ((command = input.shift()) !== "Christmas") {
- Number(command) <= 16 ? kids++ : adults++;
- }
- console.log(`Number of adults: ${adults}\nNumber of kids: ${kids}\nMoney for toys: ${kids * 5}\nMoney for sweaters: ${adults * 15}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement