Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = /(?<custm>\%[A-Z][a-z]+\%).*(?<prod>\<\w+\>).*(?<count>\|\d+\|)[^\|\$\.\%]*?(?<price>\d+[\.]\d+\$|\d+\$)/;
- let i = 0;
- let obj = {};
- let income = 0;
- while (input[i] !== 'end of shift') {
- if ((customer = pattern.exec(input[i]))) {
- let name = customer.groups.custm.split('').filter(el => /[A-Z]|[a-z]/.test(el));
- name = name.join('');
- let product = customer.groups.prod.split('').filter(el => /[A-Z]|[a-z]/.test(el));
- product = product.join('');
- let count = customer.groups.count.split('').filter(el => /\d/.test(el));
- count = count.join('');
- let price = customer.groups.price.split('$')[0];
- income += count * price;
- if (obj.hasOwnProperty(name)) {
- obj[name][product] = count * price;
- } else {
- obj[name] = { [product]: count * price };
- }
- }
- i++;
- }
- // console.log(obj, income);
- for (let key in obj) {
- for (let key2 in obj[key]) {
- console.log(`${key}: ${key2} - ${obj[key][key2].toFixed(2)}`);
- }
- }
- console.log(`Total income: ${income.toFixed(2)}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement