Home > Software design >  Hi i am having a problem in java script with function and arrays
Hi i am having a problem in java script with function and arrays

Time:03-30

Print a shopping list Create a function called print Shopping List that take a list of products and print the items to the screen.

//
 shopping List = [
    { item Name : 'Bread', price : 11.00 },
    { item Name : 'Milk', price : 7.00 },
    { item Name : 'Cheese', price : 19.50 }
];
This should print:

Shopping list:
* Bread @ R11.00
* Milk @ R7.00
* Cheese @ R19.50

CodePudding user response:

console.log('Shopping List:');
shoppingList.forEach(ele => {
console.log('*' ele.ItemName '@' 'R' ele.price);
);

Hope this helps.

CodePudding user response:

You can try the below code. Hope it helps.

const shoppingList = [{
    itemName: 'Bread',
    price: 11.00
  },
  {
    itemName: 'Milk',
    price: 7.00
  },
  {
    itemName: 'Cheese',
    price: 19.50
  }
];

const itemsHTML = shoppingList.map(item => (
  `<li>${item.itemName} @ R${item.price}</li>`
))

document.write(`<ul>${itemsHTML}</ul>`);

  • Related