Advertisement
Spocoman

Christmas Gifts

Feb 27th, 2022 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function christmasGifts(input) {
  2.     let adults = 0;
  3.     let kids = 0;
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.         let command = input[i];
  7.         if (command === "Christmas") {
  8.             break;
  9.         }
  10.         let years = Number(command);
  11.         if (years <= 16) {
  12.             kids++;
  13.         } else {
  14.             adults++;
  15.         }
  16.     }
  17.  
  18.     let kidsPrice = kids * 5;
  19.     let adultsPrice = adults * 15;
  20.  
  21.     console.log(`Number of adults: ${adults}`);
  22.     console.log(`Number of kids: ${kids}`);
  23.     console.log(`Money for toys: ${kidsPrice}`);
  24.     console.log(`Money for sweaters: ${adultsPrice}`);
  25. }
  26.  
  27. РЕШЕНИЯ С WHILE:
  28.  
  29. function christmasGifts(input) {
  30.     let command = input[0];
  31.     let index = 1;
  32.     let adults = 0;
  33.     let kids = 0;
  34.  
  35.     while (command !== "Christmas") {
  36.         let years = Number(command);
  37.         if (years <= 16) {
  38.             kids++;
  39.         } else {
  40.             adults++;
  41.         }
  42.         command = input[index++];
  43.     }
  44.  
  45.     let kidsPrice = kids * 5;
  46.     let adultsPrice = adults * 15;
  47.  
  48.     console.log(`Number of adults: ${adults}`);
  49.     console.log(`Number of kids: ${kids}`);
  50.     console.log(`Money for toys: ${kidsPrice}`);
  51.     console.log(`Money for sweaters: ${adultsPrice}`);
  52. }
  53.  
  54. Решение с тернарен оператор и леко тарикатската:)
  55.  
  56. function christmasGifts(input) {
  57.     let command;
  58.     let adults = 0;
  59.     let kids = 0;
  60.  
  61.     while ((command = input.shift()) !== "Christmas") {
  62.         Number(command) <= 16 ? kids++ : adults++;
  63.     }
  64.    
  65.     console.log(`Number of adults: ${adults}\nNumber of kids: ${kids}\nMoney for toys: ${kids * 5}\nMoney for sweaters: ${adults * 15}`);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement