Skip to the content

Project: grocery list

JavaScript · Unit 5: Arrays

Your grocery list is two arrays: items[i] and prices[i] talk about the same product, because the same index ties them together.

To read them together, you walk through with an index for.

const items = ["bread", "milk"];
const prices = [3, 4];
const n = prices.length;
for (let i = 0; i < n; i++) {
  console.log(items[i], prices[i]);
}

Prints

bread 3
milk 4

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Complete it to print the items that cost less than 10.

  2. 2. Predict the output

    You are taking 2 breads and 3 milks. What does it print?

  3. 3. Find the bug

    This program should print the three prices. Which line has the error?

  4. 4. Write the code

    Write total(prices, counts), which returns what you pay if you take counts[i] of each product at prices[i] each.

  5. 5. Multiple choice

    What does subs hold when this code is done?

  6. 6. Write the code

    Write priciest(items, prices), which returns the name of the most expensive product. The arrays never come in empty.

  7. 7. Complete the code

    Complete it to keep the prices under 10.

  8. 8. Predict the output

    These are the prices on your list. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →