Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Dock Report

Porting the Dock Report

In this project, you ported the crew’s dock report to JavaScript, ran it, and got the same five dock lines Python prints. The van’s lines go in the loop’s gaps in the exercise.

  • The list of dicts becomes an array of objects: the brackets and braces stay, and the keys lose their quotes. Each name gets a const, and the closed list is an array too.
  • The fill rate is one line of arithmetic, so it becomes an arrow in a const. The arrow returns its formula without a return.
  • classify() is a named building block, so it stays a function declaration, with braces around its body. Each elif turns into else if, and each == into ===. A lookup with a fixed key becomes a dot, so dock["bikes"] becomes dock.bikes.
  • In the closed check, Python’s in becomes closedDocks.includes(dock.name): the list first, then the value. JavaScript’s in asks about keys, which in an array are its positions, never its values. With in, Market Hall would count as empty, and the van would load bikes it shouldn’t.
  • The loop goes through the array with for...of, with a const for each dock, and status and fill get a const too, since each pass has its own.
  • The print line becomes a template literal. The format spec can’t come along, because a hole holds an expression, not a format spec, so you round inside the hole with fill.toFixed(0).

Here’s the port, dock-report.js, with the van’s lines still to come:

Language: JavaScript
const docks = [
  { name: "Elm Street", bikes: 0, slots: 12 },
  { name: "Harbor Square", bikes: 14, slots: 1 },
  { name: "Station Road", bikes: 6, slots: 8 },
  { name: "Market Hall", bikes: 0, slots: 10 },
  { name: "Riverside Park", bikes: 11, slots: 2 },
];
const closedDocks = ["Market Hall"];

const fillRate = (bikes, slots) => bikes / (bikes + slots) * 100;

function classify(dock) {
  if (closedDocks.includes(dock.name)) {
    return "closed";
  } else if (dock.bikes === 0) {
    return "empty";
  } else if (dock.slots <= 2) {
    return "nearly full";
  } else {
    return "ok";
  }
}

for (const dock of docks) {
  const status = classify(dock);
  const fill = fillRate(dock.bikes, dock.slots);
  console.log(`${dock.name}: ${status}, ${fill.toFixed(0)}%`);
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 In this project, you’ll port the crew’s dock report to JavaScript and run it. Here’s the script. It keeps the docks as a list of dicts, next to the closed list.

00:14 Two small functions work out a dock’s fill rate and its status, and a loop prints a line for each dock. The van’s lines are tucked into the loop and the end: where to collect bikes, and how many to load.

00:28 They stay folded here: the van’s lines go in the loop’s gaps in the exercise after this video.

00:36 Start with the data. The list of dicts becomes an array of objects. The brackets and braces stay, and the keys lose their quotes.

00:46 The closed list ports as an array too, and each name gets a const.

00:53 Next, the functions. The fill rate is one line of arithmetic, so it becomes an arrow in a const, the way JavaScript developers write a small formula.

01:04 The parameters come first, then the arrow, then the formula, which the arrow returns without a return.

01:13 classify() is a named building block, so it stays a function declaration, with braces around its body. Each elif turns into else if, and each == into ===.

01:26 A lookup with a fixed key becomes a dot. In the closed check, Python’s in becomes .includes(): the list first, then the value. JavaScript’s in asks about keys, which in an array are its positions, never its values.

01:44 With in, Market Hall would count as empty, and the van would load bikes it shouldn’t.

01:51 The loop walks the array with for...of, with a const for each dock. status and fill get a const too, since each pass has its own.

02:01 The print line becomes a template literal. The format spec can’t come along: a hole holds an expression, not a format spec. So you round inside the hole with .toFixed(), as you did for Harbor Square.

02:17 Now run the file, and it prints five lines. Market Hall has no bikes, but it’s on the closed list, so it comes out closed. .includes() finds Market Hall, so classify() returns at its first test, before it looks at the bikes, as Python’s chain does.

02:34 And the other four lines match Python’s, rounding and all.

02:40 So here’s how the port came together. Records are objects in an array, read with a dot. Named building blocks are function declarations, and small formulas are arrows.

02:53 Membership is .includes(), and rounding is a method call in the hole.

02:58 Next up is the exercise Finish the Dock Report. You’ll add the nearly full docks and the van’s load, printed as the crew expects.

Become a Member to join the conversation.