Skip to the content

Choosing the structure

JavaScript · Unit 23: Data structures

You now have eight structures and none of them is better than the others. What decides is the question you're going to ask the data.

An array answers "what's in slot 2?" and "how many are there?". A Set answers "is it already there?", and nothing else.

const f = ["mia", "leo", "mia"];
console.log(f[2], f.length);
const s = new Set(f);
console.log(s.has("mia"), s.size);

Prints

mia 3
true 2

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

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

  1. 1. Multiple choice

    You're walking a maze and at every junction you write down where you came in, so you can go back undoing the path: the last junction first and the first one last. Which structure is that?

  2. 2. Complete the code

    About a discount code you only need to know whether it was already used. Fill in the structure that answers that on its own.

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    Both questions come out of the same object of counts. What does it print?

  5. 5. Find the bug

    It should print how many times a pizza was ordered, 2. Which line has the error?

  6. 6. Complete the code

    The question is who is connected to whom. Fill in the structure that answers it.

  7. 7. Multiple choice

    You're building a route and every new stop goes in at the front. There will be 10,000 stops and you're never going to ask for stop number 500. What suits you?

  8. 8. Predict the output

    It's the walk from lesson 4, but taking with pop instead of shift. What does it print?

  9. 9. Put the lines in order

    Put pick(q, done) in order. It walks the line q and returns the first one that isn't in done, or "nobody" if everyone already went.

  10. 10. Find the case that fails

    orders(os) returns how many orders came in, counting repeats: two pizzas are two orders. If none came in, 0. Which call breaks it?

  11. 11. Multiple choice

    What does this program count?

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 →