Skip to content

Commit e3270c2

Browse files
committed
Rename global in Chapter 7, pass it explicitly to findRoute
1 parent aaca7de commit e3270c2

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

07_robot.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This network of roads forms a _((graph))_. A graph is a collection of
4646
points (places in the village) with lines between them (roads). This
4747
graph will be the world that our robot moves through.
4848

49-
{{index "roadsFrom object"}}
49+
{{index "roadGraph object"}}
5050

5151
That array of strings isn't very easy to work with. Instead, we'll
5252
typically want to find the destinations that we can reach from a given
@@ -70,7 +70,7 @@ function buildGraph(edges) {
7070
return graph;
7171
}
7272
73-
const roadsFrom = buildGraph(roads);
73+
const roadGraph = buildGraph(roads);
7474
```
7575

7676
Given a an array of edges, `buildGraph` creates a map object that, for
@@ -146,7 +146,7 @@ class WorldState {
146146
}
147147
148148
move(destination) {
149-
if (!roadsFrom[this.place].includes(destination)) {
149+
if (!roadGraph[this.place].includes(destination)) {
150150
return this;
151151
} else {
152152
let parcels = this.parcels.map(p => {
@@ -297,7 +297,7 @@ function randomPick(array) {
297297
}
298298
299299
function randomRobot(state) {
300-
return {direction: randomPick(roadsFrom[state.place])};
300+
return {direction: randomPick(roadGraph[state.place])};
301301
}
302302
```
303303

@@ -320,10 +320,10 @@ sophisticated robot to work.
320320
WorldState.random = function(parcelCount = 5) {
321321
let parcels = [];
322322
for (let i = 0; i < parcelCount; i++) {
323-
let address = randomPick(Object.keys(roadsFrom));
323+
let address = randomPick(Object.keys(roadGraph));
324324
let place;
325325
do {
326-
place = randomPick(Object.keys(roadsFrom));
326+
place = randomPick(Object.keys(roadGraph));
327327
} while (place == address);
328328
parcels.push({place, address});
329329
}
@@ -453,11 +453,11 @@ shortest routes, if there are more than one) to the goal.
453453
This is a function that does this:
454454

455455
```{includeCode: true}
456-
function findRoute(from, to) {
456+
function findRoute(graph, from, to) {
457457
let work = [{at: from, route: []}];
458458
for (let i = 0; i < work.length; i++) {
459459
let {at, route} = work[i];
460-
for (let place of roadsFrom[at]) {
460+
for (let place of graph[at]) {
461461
if (place == to) return route.concat(place);
462462
if (!work.some(w => w.at == place)) {
463463
work.push({at: place, route: route.concat(place)});
@@ -505,9 +505,9 @@ function goalOrientedRobot({place, parcels}, route) {
505505
if (route.length == 0) {
506506
let parcel = parcels[0];
507507
if (parcel.place != place) {
508-
route = findRoute(place, parcel.place);
508+
route = findRoute(roadGraph, place, parcel.place);
509509
} else {
510-
route = findRoute(place, parcel.address);
510+
route = findRoute(roadGraph, place, parcel.address);
511511
}
512512
}
513513
return {direction: route[0], memory: route.slice(1)};

code/solutions/07_2_robot_efficiency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ function lazyRobot({place, parcels}, route) {
33
// Describe a route for every parcel
44
let routes = parcels.map(parcel => {
55
if (parcel.place != place) {
6-
return {route: findRoute(place, parcel.place),
6+
return {route: findRoute(roadGraph, place, parcel.place),
77
pickUp: true};
88
} else {
9-
return {route: findRoute(place, parcel.address),
9+
return {route: findRoute(roadGraph, place, parcel.address),
1010
pickUp: false};
1111
}
1212
});

0 commit comments

Comments
 (0)