Advertisement
Spocoman

07. Trekking Mania

Dec 24th, 2021 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function trekkingMania(input) {
  2.     let groupNumbers = Number(input[0]);
  3.     let musala = 0;
  4.     let montBlanc = 0;
  5.     let kilimanjaro = 0;
  6.     let k2 = 0;
  7.     let everest = 0;
  8.     let alpinistSum = 0;
  9.  
  10.     for (let i = 1; i <= groupNumbers; i++) {
  11.         let group = Number(input[i]);
  12.         alpinistSum += group;
  13.         if (group > 40) {
  14.             everest += group;
  15.         } else if (group > 25) {
  16.             k2 += group;
  17.         } else if (group > 12) {
  18.             kilimanjaro += group;
  19.         } else if (group > 5) {
  20.             montBlanc += group;
  21.         } else if (group > 0) {
  22.             musala += group;
  23.         }
  24.     }
  25.  
  26.     console.log(`${(100.0 * musala / alpinistSum).toFixed(2)}%`);
  27.     console.log(`${(100.0 * montBlanc / alpinistSum).toFixed(2)}%`);
  28.     console.log(`${(100.0 * kilimanjaro / alpinistSum).toFixed(2)}%`);
  29.     console.log(`${(100.0 * k2 / alpinistSum).toFixed(2)}%`);
  30.     console.log(`${(100.0 * everest / alpinistSum).toFixed(2)}%`);
  31. }
  32.  
  33. Решение с колекция, тернарен оператор и shift():
  34.  
  35. function trekkingMania(input) {
  36.     let info = { 'musala': 0, 'montBlanc': 0, 'kilimanjaro': 0, 'k2': 0, 'everest': 0 };
  37.     let groupNumbers = Number(input.shift());
  38.     let alpinistSum = 0;
  39.  
  40.     while (groupNumbers-- !== 0) {
  41.         let group = Number(input.shift());  
  42.         info[(group > 40 ? 'everest' : group > 25 ? 'k2' : group > 12 ? 'kilimanjaro' : group > 5 ? 'montBlanc' : 'musala')] += group;
  43.         alpinistSum += group;
  44.     }
  45.  
  46.     for (var mountain in info) {
  47.         console.log(`${(100.0 * info[mountain] / alpinistSum).toFixed(2)}%`);
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement