Liliana797979

viarno reshenie add astra - final exam

Aug 9th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //(\/|#)([A-Za-z])+\1([0-9]\/|#[0-9]\/|#[0-9]\/|#)(\d)\1/g;
  2.  
  3. // ([A-Za-z\s]+)
  4. // (\d{2}\/\d{2}\/\d{2})
  5. //(\d+)
  6.  
  7. function solve(text) {
  8.     let total = 0;
  9.     let result = [];
  10.     //let pattern = /(#|\/)([A-Za-z\s]+)\1 (\d{2}\/\d{2}\/\d{2})\1(\d+)\1/g;
  11.     let pattern = /([#|])([A-Za-z\s]+)\1(\d{2}\/\d{2}\/\d{2})\1(\d{1,5})\1/g;
  12.  
  13.     let match = pattern.exec(text);
  14.     while (match != null) {
  15.         //console.log(match);
  16.         let name = match[2];
  17.         let date = match[3];
  18.         let calories = Number(match[4]);
  19.         //console.log(name, date, calories);
  20.  
  21.         total += calories;
  22.         result.push(`Item: ${name}, Best before: ${date}, Nutrition: ${calories}`);
  23.         match = pattern.exec(text);
  24.     }
  25.     let days = Math.floor(total / 2000);
  26.     //console.log(total);
  27.     //console.log(result);
  28.     console.log(`You have food to last you for: ${days} days!`);
  29.     for (let line of result) {
  30.         console.log(line);
  31.     }
  32. }
  33.  
  34. solve('#Bread#19/03/21#4000#|Invalid|03/03.20||Apples|08/10/20|200||Carrots|06/08/20|500||Not right|6.8.20|5|');
Advertisement
Add Comment
Please, Sign In to add comment