Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- var silver = 0,
- gold = 0,
- diamonds = 0;
- for (var i = 0; i < arr.length; i++) {
- var str = arr[i];
- var regex = /mine\s*.*?-\s*(silver|gold|diamonds)\s*:\s*(\d+)\s*/;
- if (!regex.test(str)) {
- continue;
- }
- var split = str.match(regex);
- var ammount = parseInt(split[2]);
- var material = split[1];
- switch (material) {
- case 'silver':
- silver += ammount;
- break;
- case 'gold':
- gold += ammount;
- break;
- case 'diamonds':
- diamonds += ammount;
- break;
- }
- }
- console.log('*Silver: %d', silver);
- console.log('*Gold: %d', gold);
- console.log('*Diamonds: %d', diamonds);
- }
- solve(["mine bobovdol - gold: 10",
- "mine - diamonds: 5",
- 'mine colas - wood: 10',
- 'mine myMine - silver: 14',
- 'mine silver:14 - silver: 14'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement