Advertisement
TZinovieva

Ad Astra JS Fundamentals

Mar 25th, 2023
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(text) {
  2.     let regex = /([|#])(?<food>[a-z\sA-Z]+)\1(?<expDate>(\d{2})\/\d{2}\/\d{2})\1(?<calories>\d+)\1/g;
  3.     let days = 0;
  4.     let totalCalories = 0;
  5.     let buff = '';
  6.     while ((matches = regex.exec(text)) !== null) {
  7.         let foodName = matches.groups['food'];
  8.         let expiryDate = matches.groups['expDate'];
  9.         let calories = Number(matches.groups['calories']);
  10.         totalCalories += calories;
  11.         buff += `Item: ${foodName}, Best before: ${expiryDate}, Nutrition: ${calories}\n`;
  12.     }
  13.     days = Math.floor(totalCalories / 2000);
  14.     console.log(`You have food to last you for: ${days} days!`);
  15.  
  16.     console.log(buff);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement