Advertisement
kstoyanov

02. Ad Astra js exam

Aug 15th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function solve(args) {
  3.   const str = args.shift().trim();
  4.   const pat = /([#|/]{1})(?<itemName>[a-zA-Z ]+)\1(?<exDate>[0-9]{2}\/[0-9]{2}\/[0-9]{2})\1(?<calories>\d+)\1/gi;
  5.  
  6.   const takeMatch = str.match(pat);
  7.  
  8.   if (takeMatch !== null) {
  9.     let sumCal = 0;
  10.     takeMatch.forEach((el) => {
  11.       if (el && el.includes('#')) {
  12.         const [,,, calories] = el.split('#');
  13.  
  14.         sumCal += Number(calories);
  15.       } else if (el.includes('|')) {
  16.         const [,,, calories] = el.split('|');
  17.         sumCal += Number(calories);
  18.       }
  19.     });
  20.     const days = sumCal / 2000;
  21.     console.log(`You have food to last you for: ${Math.floor(days)} days!`);
  22.  
  23.     takeMatch.forEach((el) => {
  24.       if (el && el.includes('#')) {
  25.         const [, foodName, exDate, calories] = el.split('#');
  26.  
  27.         console.log(`Item: ${foodName}, Best before: ${exDate}, Nutrition: ${calories}`);
  28.       } else if (el.includes('|')) {
  29.         const [, foodName, exDate, calories] = el.split('|');
  30.         console.log(`Item: ${foodName}, Best before: ${exDate}, Nutrition: ${calories}`);
  31.       }
  32.     });
  33.   } else {
  34.     console.log('You have food to last you for: 0 days!');
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement