Guest User

Problem1-UncleScroogesBag

a guest
Apr 5th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     var index,
  3.         elements = [],
  4.         type,
  5.         value,
  6.         totalCoins = 0,
  7.         validNum,
  8.         goldCoins,
  9.         silverCoins,
  10.         bronzeCoins;
  11.  
  12.     for (var index in input) {
  13.         elements = input[index].split(' ');
  14.         type = elements[0].toLowerCase();
  15.         value = Number(elements[1]);
  16.  
  17.         //check if the value is a valid positive integer number
  18.         validNum = false;
  19.         if (value % 1 === 0 && value > 0) {
  20.             validNum = true;
  21.         }
  22.  
  23.         //check if the type of the element is a coin
  24.         //and if its value is a valid number
  25.         if ((type === 'coin' || type === 'coins') && validNum === true) {
  26.             totalCoins += value;
  27.         }
  28.     }
  29.  
  30.     //calculate the number of gold, silver and bronze coins
  31.     goldCoins = Math.floor(totalCoins / 100);
  32.     silverCoins = Math.floor((totalCoins % 100) / 10);
  33.     bronzeCoins = totalCoins % 10;
  34.  
  35.     //print the result
  36.     console.log('gold : ' + goldCoins);
  37.     console.log('silver : ' + silverCoins);
  38.     console.log('bronze : ' + bronzeCoins);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment