Advertisement
Spocoman

PC Game Shop

Mar 15th, 2022 (edited)
84
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pcGameShop(input) {
  2.     let volume = Number(input[0]);
  3.     let hearthstones = 0,
  4.         fornites = 0,
  5.         overwatches = 0,
  6.         others = 0;
  7.  
  8.     for (let i = 1; i <= volume; i++) {
  9.         let game = input[i];
  10.         switch (game) {
  11.             case "Hearthstone":
  12.                 hearthstones++;
  13.                 break;
  14.             case "Fornite":
  15.                 fornites++;
  16.                 break;
  17.             case "Overwatch":
  18.                 overwatches++;
  19.                 break;
  20.             default:
  21.                 others++;
  22.                 break;
  23.         }
  24.     }
  25.  
  26.     console.log(`Hearthstone - ${(hearthstones / volume * 100).toFixed(2)}%`);
  27.     console.log(`Fornite - ${(fornites / volume * 100).toFixed(2)}%`);
  28.     console.log(`Overwatch - ${(overwatches / volume * 100).toFixed(2)}%`);
  29.     console.log(`Others - ${(others / volume * 100).toFixed(2)}%`);
  30. }
  31.  
  32. РЕШЕНИЕ С РЕЧНИК, ТЕРНАРЕН ОПЕРАТОР И FOR IN:
  33.  
  34. function pcGameShop(input) {
  35.     let products = { 'Hearthstone': 0, 'Fornite': 0, 'Overwatch': 0, 'Others': 0 };
  36.     let volume = Number(input[0]);
  37.  
  38.     for (let i = 1; i <= volume; i++) {
  39.         let game = input[i];
  40.         products[game in products ? game : 'Others']++;
  41.     }
  42.  
  43.     for (let product in products) {
  44.         console.log(`${product} - ${(products[product] / volume * 100).toFixed(2)}%`);
  45.     }
  46. }
  47.  
  48. И леко тарикатската:)
  49.  
  50. function pcGameShop(input) {
  51.     let products = { 'Hearthstone': 0, 'Fornite': 0, 'Overwatch': 0, 'Others': 0 };
  52.  
  53.     for (let i = 1; i < input.length; i++) {
  54.         products[input[i] in products ? input[i] : 'Others']++;
  55.     }
  56.  
  57.     for (let product in products) {
  58.         console.log(`${product} - ${(products[product] / Number(input[0]) * 100).toFixed(2)}%`);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement