Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = />>(?<name>\w+)<<(?<price>\d+[.]?\d+)!(?<quantity>\d+)/g
- let totalMoney = 0;
- let furnitures = [];
- let line = input.shift();
- while (line != 'Purchase') {
- let result;
- if (line.match(pattern)) {
- result = pattern.exec(line);
- furnitures.push(result.groups.name);
- totalMoney += (result.groups.price * result.groups.quantity);
- }
- line = input.shift();
- }
- console.log('Bought furniture:');
- console.log(`${furnitures.join('\n')}`);
- console.log(`Total money spend: ${totalMoney.toFixed(2)}`);
- }
- solve(['>>Sofa<<100.22!3',
- '>>TV<<200.1055!2',
- '>Invalid<<!5',
- 'Purchase',
- ])
Advertisement
Add Comment
Please, Sign In to add comment