Advertisement
vladovip

JS FUND FINEX_ Add Astronaut_AdAstra

May 8th, 2022
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(inputArr) {
  2.  
  3.     let text = inputArr[0];
  4.     let pattern = /(#|\|)(?<ItemName>[A-Za-z\s]+)\1(?<Date>\d{2}\/\d{2}\/\d{2})\1(?<Calories>\d{1,5})\1/g
  5.     let allMatches = pattern.exec(text);
  6.     let totalCalories = 0;
  7.     let foodInfoArr = [];
  8.    
  9.     while( allMatches != null  ){
  10.         let FoodName = allMatches.groups.ItemName;
  11.         let expirationDate = allMatches.groups.Date;
  12.         let currentCalories = allMatches.groups.Calories;
  13.        
  14.         foodInfoArr.push(`Item: ${FoodName}, Best before: ${expirationDate}, Nutrition: ${currentCalories}`)
  15.         totalCalories += Number(currentCalories);
  16.         allMatches = pattern.exec(text);
  17.     }
  18.  
  19.      let days =  Math.floor (totalCalories /  2000) ;
  20.  
  21.      if ( days > 0 ){
  22.          console.log(`You have food to last you for: ${days} days!`);
  23.          console.log(foodInfoArr.join("\n"));
  24.       } else {
  25.          console.log(`You have food to last you for: ${days} days!`);
  26.      }
  27.  
  28. }
  29.  
  30. adAstra([
  31.   "#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|",
  32. ]);
  33.  
  34. console.log(`************`);
  35.  
  36. adAstra([
  37.   "$$#@@%^&#Fish#24/12/20#8500#|#Incorrect#19.03.20#450|$5*(@!#Ice Cream#03/10/21#9000#^#@aswe|Milk|05/09/20|2000|",
  38. ]);
  39.  
  40. console.log(`***********`);
  41.  
  42. adAstra(['Hello|#Invalid food#19/03/20#450|$5*(@' ]);
  43.  
  44.  
  45. // (#|\|)(?<ItemName>[A-Za-z\s]+)\1(?<Date>\d+\/\d+\/\d+)\1(?<Calories>\d+)\1
  46.  
  47. // (#|\|)(?<ItemName>[A-Za-z\s]+)\1(?<Date>\d{2}\/\d{2}\/\d{2})\1(?<Calories>\d{1,5})\1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement