Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function trekkingMania(input) {
- let groupNumbers = Number(input[0]);
- let musala = 0;
- let montBlanc = 0;
- let kilimanjaro = 0;
- let k2 = 0;
- let everest = 0;
- let alpinistSum = 0;
- for (let i = 1; i <= groupNumbers; i++) {
- let group = Number(input[i]);
- alpinistSum += group;
- if (group > 40) {
- everest += group;
- } else if (group > 25) {
- k2 += group;
- } else if (group > 12) {
- kilimanjaro += group;
- } else if (group > 5) {
- montBlanc += group;
- } else if (group > 0) {
- musala += group;
- }
- }
- console.log(`${(100.0 * musala / alpinistSum).toFixed(2)}%`);
- console.log(`${(100.0 * montBlanc / alpinistSum).toFixed(2)}%`);
- console.log(`${(100.0 * kilimanjaro / alpinistSum).toFixed(2)}%`);
- console.log(`${(100.0 * k2 / alpinistSum).toFixed(2)}%`);
- console.log(`${(100.0 * everest / alpinistSum).toFixed(2)}%`);
- }
- Решение с колекция, тернарен оператор и shift():
- function trekkingMania(input) {
- let info = { 'musala': 0, 'montBlanc': 0, 'kilimanjaro': 0, 'k2': 0, 'everest': 0 };
- let groupNumbers = Number(input.shift());
- let alpinistSum = 0;
- while (groupNumbers-- !== 0) {
- let group = Number(input.shift());
- info[(group > 40 ? 'everest' : group > 25 ? 'k2' : group > 12 ? 'kilimanjaro' : group > 5 ? 'montBlanc' : 'musala')] += group;
- alpinistSum += group;
- }
- for (var mountain in info) {
- console.log(`${(100.0 * info[mountain] / alpinistSum).toFixed(2)}%`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement