vladovip

Ad Astra _ Exam Preparation JS Fundamentals

Apr 2nd, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(input) {
  2.   let text = input[0];
  3.  
  4.   let allLineRegExp = /[#|][A-Z\sa-z]+[#|]\d\d\/\d\d\/\d\d[#|]\d+[#|]/g;
  5.  
  6.   //  let validlineContains = allLineRegExp.test(text);
  7.   //  console.log(validlineContains);
  8.   let matchesArr = text.match(allLineRegExp);
  9.   // console.log(matchesArr);
  10.   let totalcallories = 0;
  11.   let ListOfProduts = [];
  12.  
  13.   for (let foodLines of matchesArr) {
  14.     let splitingOperatorRegExp = /[#|]/g;
  15.     tokens = foodLines.split(splitingOperatorRegExp);
  16.  
  17.     let foodItem = tokens[1];
  18.     let expireDate = tokens[2];
  19.     let foodkCal = Number(tokens[3]);
  20.  
  21.     if (foodkCal <= 10000 && foodkCal > 0) {
  22.       totalcallories += foodkCal;
  23.       let eachFoodItem = {
  24.         foodItem,
  25.         expireDate,
  26.         foodkCal,
  27.       };
  28.       ListOfProduts.push(eachFoodItem);
  29.     }
  30.    
  31.   }
  32.   let foodDays = Math.floor(totalcallories / 2000);
  33.   console.log(`You have food to last you for: ${foodDays} days!`);
  34.  
  35.   for (let itemLine of ListOfProduts) {
  36.     console.log(
  37.       `Item: ${itemLine.foodItem}, Best before: ${itemLine.expireDate}, Nutrition: ${itemLine.foodkCal}`
  38.     );
  39.   }
  40. }
  41.  
  42.  
  43.  
  44. adAstra([
  45.   "#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|",
  46. ]);
  47.  
  48. console.log(`----------`);
  49.  
  50. adAstra([
  51.   "$$#@@%^&#Fish#24/12/20#8500#|#Incorrect#19.03.20#450|$5*(@!#Ice Cream#03/10/21#9000#^#@aswe|Milk|05/09/20|2000|",
  52. ]);
  53.  
  54. // You must extract the information about the food and calculate the total calories.
  55.  
  56. //#{item name}#{expiration date}#{calories}#  OR
  57.  
  58. // |{item name}|{expiration date}|{calories}|
  59.  
  60. // Calculate the total calories of all food items and
  61. // then determine how many days you can last with the food you have.
  62. //Keep in mind that you need 2000kcal a day.
  63.  
  64. // [#|][A-Za-z]+[#|]\d\d\/\d\d\/\d\d[#|]\d+[#|]    all Food Item Line Info
  65. // [#|][A-Z\sa-z]+[#|]\d\d\/\d\d\/\d\d[#|]\d+[#|] - second option all fodd Line Ifno
  66.  
Advertisement
Add Comment
Please, Sign In to add comment