Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- var index,
- elements = [],
- type,
- value,
- totalCoins = 0,
- validNum,
- goldCoins,
- silverCoins,
- bronzeCoins;
- for (var index in input) {
- elements = input[index].split(' ');
- type = elements[0].toLowerCase();
- value = Number(elements[1]);
- //check if the value is a valid positive integer number
- validNum = false;
- if (value % 1 === 0 && value > 0) {
- validNum = true;
- }
- //check if the type of the element is a coin
- //and if its value is a valid number
- if ((type === 'coin' || type === 'coins') && validNum === true) {
- totalCoins += value;
- }
- }
- //calculate the number of gold, silver and bronze coins
- goldCoins = Math.floor(totalCoins / 100);
- silverCoins = Math.floor((totalCoins % 100) / 10);
- bronzeCoins = totalCoins % 10;
- //print the result
- console.log('gold : ' + goldCoins);
- console.log('silver : ' + silverCoins);
- console.log('bronze : ' + bronzeCoins);
- }
Advertisement
Add Comment
Please, Sign In to add comment