".route" ? (from Eloquent Javascript) Is this some Javascript method or ES6 notation?

Hello, everyone. I’m working through Eloquent JavaScript and I got really, really stuck on a problem so I gave in and looked at the answer but there’s something in the answer I don’t understand.

First, the answer:

function lazyRobot({place, parcels}, route) {
  if (route.length == 0) {
    // Describe a route for every parcel
    let routes = parcels.map(parcel => {
      if (parcel.place != place) {
        return {route: findRoute(roadGraph, place, parcel.place),
                pickUp: true};
      } else {
        return {route: findRoute(roadGraph, place, parcel.address),
                pickUp: false};
      }
    });

    // This determines the precedence a route gets when choosing.
    // Route length counts negatively, routes that pick up a package
    // get a small bonus.
    function score({route, pickUp}) {
      return (pickUp ? 0.5 : 0) - route.length;
    }
    route = routes.reduce((a, b) => score(a) > score(b) ? a : b).route;
  }

  return {direction: route[0], memory: route.slice(1)};
}

runRobotAnimation(VillageState.random(), lazyRobot, []);

The part I don’t understand is .route after the end of the reduce function. Can anyone help me figure out what this does? Thank you!

Edit: Here’s the findRoute function for reference

function findRoute(graph, from, to) {
  let work = [{at: from, route: []}];
  for (let i = 0; i < work.length; i++) {
    let {at, route} = work[i];
    for (let place of graph[at]) {
      if (place == to) return route.concat(place);
      if (!work.some(w => w.at == place)) {
        work.push({at: place, route: route.concat(place)});
      }
    }
  }
}

so the .route you refer to is a property on the reduced array. As (I believe) the routes array is reduced to a single member, that member has a .route property. And the variable route is set to that property value.

Which chapter, and what context? With the bit of code you’ve supplied, it feels like there’s a lot of context missing.

Well, it’s one of the exercises and there’s a lot of functions at play but it’s from Chapter 7. http://eloquentjavascript.net/07_robot.html

Right, ok. so in the findRoute() you provide (I had to skim the chapter for context), the work array is being created. In that, each member contains an object that looks like:

{
  at:       place,
  route:  []  // route is an array, indicating the path to get to this place.
}

Thus, when the reduced is called, based on the comparitive values of score(a) and score(b), the final remaining member of that array (reduce is returning a single entity), contains those same two properties: at and route.

So, the line

route = routes.reduce((a, b) => score(a) > score(b) ? a : b).route;

is saying “set route equal to the route property of the single member returned by this reduce function”.

1 Like

Thanks for that very clear explanation. I still need to do some thinking about my own code (which I didn’t post) and why it’s not working, but I definitely feel clear about what “.route” is doing there. Thank you very much!

1 Like

Glad I could help, and best of luck!

1 Like