georgiev955

Ad Astra

Jul 15th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(input) {
  2.     let pattern = /([|#])(?<itemName>[A-Za-z ]+)\1(?<exp>[\d]{2}\/[\d]{2}\/[\d]{2})\1(?<cal>\d+)\1/gm;
  3.     let text = input[0];
  4.     let totalCalories = 0;
  5.     let result = [];
  6.  
  7.     let matches = pattern.exec(text);
  8.     while(matches !== null) {
  9.         let info = [];
  10.         let itemName = matches.groups.itemName;
  11.         let expDate = matches.groups.exp;
  12.         let calories = matches.groups.cal;
  13.         info.push(itemName,expDate,calories);
  14.         result.push(info);
  15.         totalCalories += Number(calories);
  16.  
  17.         matches = pattern.exec(text);
  18.     }
  19.  
  20.     let days = Math.floor(totalCalories/2000);
  21.     console.log(`You have food to last you for: ${days} days!`);
  22.     for(const array of result) {
  23.         console.log(`Item: ${array[0]}, Best before: ${array[1]}, Nutrition: ${array[2]}`);
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment