Advertisement
Mvelchev

Untitled

Oct 6th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     // Write a function that composes an object by given properties. The input comes as an array of strings. Every even index of the array represents the name of the food. Every odd index is a number that is equal to the calories in 100 grams of the given product. Assign each value to its corresponding property, and finally print the object.
  3.     // The input comes as an array of string elements.
  4.     // The output should be printed on the console.
  5.    
  6.  
  7.     let calorieObject = {};
  8.  
  9.   input.forEach((el,i)=>{
  10.     if (i % 2 === 0){
  11.         calorieObject[el] = Number (i + 1)
  12.     }
  13.   });
  14.   console.log(calorieObject);
  15. }
  16.  
  17. solve(["Yoghurt", "48", "Rise", "138", "Apple", "52"]);
  18. console.log("------------------------");
  19. solve(["Yoghurt", "48", "Rise", "138", "Apple", "52"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement