Sikau

JavaScript · Day 35 · about 30 min

Project: Grocery list

Add, remove and total your shopping.

Off to the bazaar

Let's build a little grocery list manager. It keeps item names and prices in two arrays, and gives you functions to add items, remove them and work out the total.

Two lists side by side

const items = ["rice", "lentils"];
const prices = [150, 120];

for (let i = 0; i < items.length; i++) {
  console.log(`${items[i]}: Rs ${prices[i]}`);
}

In Unit 7 you'll learn a neater way — objects — to keep a name and price together. For now, two arrays work fine.

The rest of this lesson

  • Keeping two arrays in stepexplain
  • Planning the functionsexplain
  • The -1 trappredict
  • Total with reducefill
  • Build the grocery listchallenge
  • Badge quizquiz
  • Unit 6 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript