Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- const str = args.shift().trim();
- const pat = /([#|/]{1})(?<itemName>[a-zA-Z ]+)\1(?<exDate>[0-9]{2}\/[0-9]{2}\/[0-9]{2})\1(?<calories>\d+)\1/gi;
- const takeMatch = str.match(pat);
- if (takeMatch !== null) {
- let sumCal = 0;
- takeMatch.forEach((el) => {
- if (el && el.includes('#')) {
- const [,,, calories] = el.split('#');
- sumCal += Number(calories);
- } else if (el.includes('|')) {
- const [,,, calories] = el.split('|');
- sumCal += Number(calories);
- }
- });
- const days = sumCal / 2000;
- console.log(`You have food to last you for: ${Math.floor(days)} days!`);
- takeMatch.forEach((el) => {
- if (el && el.includes('#')) {
- const [, foodName, exDate, calories] = el.split('#');
- console.log(`Item: ${foodName}, Best before: ${exDate}, Nutrition: ${calories}`);
- } else if (el.includes('|')) {
- const [, foodName, exDate, calories] = el.split('|');
- console.log(`Item: ${foodName}, Best before: ${exDate}, Nutrition: ${calories}`);
- }
- });
- } else {
- console.log('You have food to last you for: 0 days!');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement