Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pcGameShop(input) {
- let volume = Number(input[0]);
- let hearthstones = 0,
- fornites = 0,
- overwatches = 0,
- others = 0;
- for (let i = 1; i <= volume; i++) {
- let game = input[i];
- switch (game) {
- case "Hearthstone":
- hearthstones++;
- break;
- case "Fornite":
- fornites++;
- break;
- case "Overwatch":
- overwatches++;
- break;
- default:
- others++;
- break;
- }
- }
- console.log(`Hearthstone - ${(hearthstones / volume * 100).toFixed(2)}%`);
- console.log(`Fornite - ${(fornites / volume * 100).toFixed(2)}%`);
- console.log(`Overwatch - ${(overwatches / volume * 100).toFixed(2)}%`);
- console.log(`Others - ${(others / volume * 100).toFixed(2)}%`);
- }
- РЕШЕНИЕ С РЕЧНИК, ТЕРНАРЕН ОПЕРАТОР И FOR IN:
- function pcGameShop(input) {
- let products = { 'Hearthstone': 0, 'Fornite': 0, 'Overwatch': 0, 'Others': 0 };
- let volume = Number(input[0]);
- for (let i = 1; i <= volume; i++) {
- let game = input[i];
- products[game in products ? game : 'Others']++;
- }
- for (let product in products) {
- console.log(`${product} - ${(products[product] / volume * 100).toFixed(2)}%`);
- }
- }
- И леко тарикатската:)
- function pcGameShop(input) {
- let products = { 'Hearthstone': 0, 'Fornite': 0, 'Overwatch': 0, 'Others': 0 };
- for (let i = 1; i < input.length; i++) {
- products[input[i] in products ? input[i] : 'Others']++;
- }
- for (let product in products) {
- console.log(`${product} - ${(products[product] / Number(input[0]) * 100).toFixed(2)}%`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement