ブラックフライデー、2日延長ブラックフライデー延長中ブラックフライデー:Proの全部が$19(通常$49)、1年でいちばん安い価格です。。 · $19 通常$49終了まで延長 · 終了まで終了まで Proにする · $19Proにする
学ぶ 練習 デイリーゴルフ リファレンス
7ステップ中1
縦の移動·レッスン 6/6·9分·練習問題 0/4Pro

使われていないコードを消す

トラック4の全部を、時間を計って。縦の移動を全部使ってlegacy.jsを見て回り、最後に残った不要な関数3つをd}で切り取ります。

手に入るキーG}d}Ctrl-dH
DEAD CODEの幕と4時の時計の下、忍者がd}の札のはさみで、巨大なコードの本からクモの巣の張った灰色のかたまりを切り取り、ゴミ箱に落としている。周りには13jの弧、Ctrl-dの札でめくれるページ、Gの裏表紙。
場面

金曜日、午後4時。legacy.jsに、死んだ関数がまだ3つあります。古いレシートプリンター、2019年の「今月のブリトー」、誰も使わなかったCSVの書き出し。チームは週末の前に片付けたくて、その前に残すものをざっと見て回りたいそうです。

1時間、290行、そして長いファイルを渡る方法の全部で。

7ステップ中2
思い出す練習

これまでのチャプターで準備運動

前のチャプターから2つ。新しい話の前に、思い出して答えましょう。2回間違えると答えが出ます。

どのキー?
チームが「画面の真ん中の」と言う。
キーを押す…下をタップ
7ステップ中3
パート1:道順

わかっていることで、キーを選ぶ

金曜日のlegacy.js、今週のキー全部で。riderReportは242行目。行きましょう。
定規
ページ番号
切れ目
めくる
ずらす
ページの中

行について何がわかっているかで、キーが決まります。定規の距離なら10j。行番号なら242G、表紙と裏表紙ならggとG。次の関数なら}。もっと先のどこかならCtrl-dかCtrl-f。開いたページの中ならH M L。

今日は新しいキーなし。今週の全部を、時間を計って。

7ステップ中4
パート2:見て回る

行番号を言われたら、Gで開く

チームは行き先を行番号で言うので、ページ番号でどこからでもまっすぐ開けます:242G。近くなら、定規か、切れ目1つ2つのほうが早いこともあります。

ページの下のほうに着いた?zzで行が真ん中に来て、みんなが読めます。ページは動き、カーソルはそのままです。

1// legacy.js — Byte Burrito, 2019–present. Please don't add anything here.
1// (Everyone has added something here.)
2
3const TAX_RATE = 0.0875;
4const MAX_DISTANCE_KM = 8;
5const OPEN_HOUR = 11;
6const CLOSE_HOUR = 23;
7const RUSH_START = 12;
8const RUSH_END = 14;
9
10function isOpen(now) {
11 const hour = now.getHours();
12 return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
13}
14
15function isLunchRush(now) {
16 const hour = now.getHours();
17 return hour >= RUSH_START && hour < RUSH_END;
18}
19
20function formatPrice(cents) {
21 const dollars = Math.floor(cents / 100);
22 const rest = String(cents % 100).padStart(2, "0");
23 return "$" + dollars + "." + rest;
24}
25
26function subtotal(items) {
27 let sum = 0;
28 for (const item of items) {
29 sum += item.price * item.qty;
30 }
31 return sum;
32}
33
34function taxFor(amount) {
35 return Math.round(amount * TAX_RATE);
36}
37
38function totalFor(order) {
39 const base = subtotal(order.items);
40 const tax = taxFor(base);
41 const tip = order.tip || 0;
42 return base + tax + tip;
43}
44
45function validateOrder(order) {
46 if (!order.items || order.items.length === 0) return "empty";
47 if (order.items.some((i) => i.qty <= 0)) return "bad quantity";
48 if (!order.address) return "no address";
49 if (!isOpen(new Date())) return "closed";
50 return null;
51}
52
53function applyCoupon(order, code) {
54 const coupon = COUPONS[code];
55 if (!coupon) return order;
56 if (coupon.expires < Date.now()) return order;
57 const off = Math.round(subtotal(order.items) * coupon.percent);
58 return { ...order, discount: off };
59}
60
61function studentDiscount(order, user) {
62 if (!user.isStudent) return 0;
63 return Math.round(subtotal(order.items) * 0.1);
64}
65
66function distanceKm(a, b) {
67 const dLat = (b.lat - a.lat) * 111;
68 const dLng = (b.lng - a.lng) * 111 * Math.cos((a.lat * Math.PI) / 180);
69 return Math.sqrt(dLat * dLat + dLng * dLng);
70}
71
72function nearestRider(riders, address) {
73 let best = null;
74 for (const rider of riders) {
75 if (!rider.online) continue;
76 const d = distanceKm(rider.position, address.position);
77 if (d > MAX_DISTANCE_KM) continue;
78 if (!best || d < best.distance) best = { rider, distance: d };
79 }
80 return best;
81}
82
83function assignRider(order, riders) {
84 const match = nearestRider(riders, order.address);
85 if (!match) return refund(order, "No rider nearby, sorry!");
86 order.riderId = match.rider.id;
87 order.eta = Math.ceil(match.distance * 4) + 10;
88 notify(match.rider.phone, "New order: " + order.id);
89 return order;
90}
91
92function etaMinutes(order, now) {
93 const rush = isLunchRush(now) ? 10 : 0;
94 return order.eta + rush;
95}
96
97function refund(order, reason) {
98 order.status = "refunded";
99 order.refundReason = reason;
100 payments.refund(order.paymentId, totalFor(order));
101 notify(order.customerPhone, "Refunded: " + reason);
102 return order;
103}
104
105function markDelivered(order, now) {
106 order.status = "delivered";
107 order.deliveredAt = now.toISOString();
108 const late = now - new Date(order.placedAt) > order.eta * 60000;
109 if (late) order.lateBy = Math.round((now - new Date(order.placedAt)) / 60000 - order.eta);
110 return order;
111}
112
113function kitchenTicket(order) {
114 const lines = order.items.map((i) => i.qty + " x " + i.name);
115 lines.unshift("ORDER " + order.id);
116 if (order.notes) lines.push("NOTE: " + order.notes);
117 return lines.join("\n");
118}
119
120function printTicket(order) {
121 const ticket = kitchenTicket(order);
122 printer.print(ticket);
123 log("printed", order.id);
124}
125
126// DEAD CODE: the old receipt printer, replaced by email receipts
127function printReceipt(order) {
128 const lines = order.items.map((i) => i.name + " " + formatPrice(i.price * i.qty));
129 lines.push("TOTAL " + formatPrice(totalFor(order)));
130 receiptPrinter.print(lines.join("\n"));
131}
132
133function emailReceipt(order, user) {
134 const body = renderReceipt(order);
135 mailer.send(user.email, "Your Byte Burrito receipt", body);
136}
137
138function menuItem(id) {
139 const item = MENU.find((m) => m.id === id);
140 if (!item) throw new Error("not on the menu: " + id);
141 return item;
142}
143
144function isSpicy(id) {
145 return menuItem(id).spicy === true;
146}
147
148function happyHourPrice(item, now) {
149 const hour = now.getHours();
150 if (hour >= 17 && hour < 19) return Math.round(item.price * 0.8);
151 return item.price;
152}
153
154function priceList(now) {
155 return MENU.map((item) => ({
156 id: item.id,
157 name: item.name,
158 price: formatPrice(happyHourPrice(item, now)),
159 }));
160}
161
162// DEAD CODE: the 2019 "burrito of the month", never shipped
163function burritoOfTheMonth(month) {
164 const picks = ["al-pastor", "veggie", "carnitas", "birria"];
165 return menuItem(picks[month % picks.length]);
166}
167
168function placeOrder(cart, user, now) {
169 const order = { id: newId(), items: cart.items, address: user.address, placedAt: now.toISOString() };
170 const problem = validateOrder(order);
171 if (problem) throw new Error(problem);
172 const fee = deliveryFee(order, distanceKm(KITCHEN, order.address));
173 order.discount = studentDiscount(order, user);
174 order.total = totalFor(order) - order.discount + fee;
175 return order;
176}
177
178function deliveryFee(order, km) {
179 const base = km <= 2 ? 0 : 1.5;
180 const perKm = km > 2 ? (km - 2) * 0.4 : 0;
181 const rush = isLunchRush(new Date()) ? 1 : 0;
182 return Math.round((base + perKm + rush) * 100) / 100;
183}
184
185function newId() {
186 return "BB-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 6);
187}
188
189function log(event, ...details) {
190 console.log("[" + new Date().toISOString() + "] " + event, ...details);
191}
192
193function notify(phone, text) {
194 if (!phone) return;
195 sms.send(phone, text.slice(0, 160));
196}
197
198function saveUser(user) {
199 db.users.put(user.id, user);
200}
201
202function loadUser(id) {
203 return db.users.get(id);
204}
205
206function saveOrder(order) {
207 db.orders.put(order.id, order);
208 log("saved", order.id);
209}
210
211function loadOrder(id) {
212 return db.orders.get(id);
213}
214
215function ordersToday(now) {
216 const day = now.toISOString().slice(0, 10);
217 return db.orders.all().filter((o) => o.placedAt.startsWith(day));
218}
219
220function revenueToday(now) {
221 return ordersToday(now).reduce((sum, o) => sum + o.total, 0);
222}
223
224function busiestHour(now) {
225 const counts = new Array(24).fill(0);
226 for (const o of ordersToday(now)) counts[new Date(o.placedAt).getHours()]++;
227 return counts.indexOf(Math.max(...counts));
228}
229
230// DEAD CODE: the CSV export the accountant asked for, then never used
231function exportCsv(orders) {
232 const rows = orders.map((o) => [o.id, o.total, o.status].join(","));
233 rows.unshift("id,total,status");
234 return rows.join("\n");
235}
236
237function ridersOnline(riders) {
238 return riders.filter((r) => r.online).length;
239}
240
241function riderReport(riders, now) {
242 const online = ridersOnline(riders);
243 const rush = isLunchRush(now);
244 return { online, rush, short: rush && online < 6 };
245}
246
247function weatherDelay(weather) {
248 if (weather === "rain") return 8;
249 if (weather === "snow") return 20;
250 return 0;
251}
252
253function adjustEta(order, weather) {
254 order.eta = order.eta + weatherDelay(weather);
255 return order;
256}
257
258function cancelOrder(order, reason) {
259 if (order.status === "delivered") return order;
260 order.status = "cancelled";
261 return refund(order, reason);
262}
263
264function reorder(previous, user, now) {
265 const cart = { items: previous.items.map((i) => ({ ...i })) };
266 return placeOrder(cart, user, now);
267}
268
269function favourite(user) {
270 const counts = {};
271 for (const o of db.orders.byUser(user.id)) {
272 for (const i of o.items) counts[i.id] = (counts[i.id] || 0) + i.qty;
273 }
274 return Object.keys(counts).sort((a, b) => counts[b] - counts[a])[0];
275}
276
277module.exports = {
278 isOpen,
279 placeOrder,
280 assignRider,
281 markDelivered,
282 printTicket,
283 emailReceipt,
284 priceList,
285 revenueToday,
286 riderReport,
287 cancelOrder,
288 reorder,
289};
NORMALエディタを読み込み中…行1、列:1
ボス戦

ステージ1 · 見て回る

4時5分。間違えて切らないように、消す前に、残す関数を6つ見ておきたいそうです。順番はチームの好きなように、ファイルのあちこち。問題の欄に、行番号と、残す理由が出ます。どんな縦の動きでもかまいません。

242行目 · riderReportは残す:配車の画面legacy.js
1// legacy.js — Byte Burrito, 2019–present. Please don't add anything here.
1// (Everyone has added something here.)
2
3const TAX_RATE = 0.0875;
4const MAX_DISTANCE_KM = 8;
5const OPEN_HOUR = 11;
6const CLOSE_HOUR = 23;
7const RUSH_START = 12;
8const RUSH_END = 14;
9
10function isOpen(now) {
11 const hour = now.getHours();
12 return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
13}
14
15function isLunchRush(now) {
16 const hour = now.getHours();
17 return hour >= RUSH_START && hour < RUSH_END;
18}
19
20function formatPrice(cents) {
21 const dollars = Math.floor(cents / 100);
22 const rest = String(cents % 100).padStart(2, "0");
23 return "$" + dollars + "." + rest;
24}
25
26function subtotal(items) {
27 let sum = 0;
28 for (const item of items) {
29 sum += item.price * item.qty;
30 }
31 return sum;
32}
33
34function taxFor(amount) {
35 return Math.round(amount * TAX_RATE);
36}
37
38function totalFor(order) {
39 const base = subtotal(order.items);
40 const tax = taxFor(base);
41 const tip = order.tip || 0;
42 return base + tax + tip;
43}
44
45function validateOrder(order) {
46 if (!order.items || order.items.length === 0) return "empty";
47 if (order.items.some((i) => i.qty <= 0)) return "bad quantity";
48 if (!order.address) return "no address";
49 if (!isOpen(new Date())) return "closed";
50 return null;
51}
52
53function applyCoupon(order, code) {
54 const coupon = COUPONS[code];
55 if (!coupon) return order;
56 if (coupon.expires < Date.now()) return order;
57 const off = Math.round(subtotal(order.items) * coupon.percent);
58 return { ...order, discount: off };
59}
60
61function studentDiscount(order, user) {
62 if (!user.isStudent) return 0;
63 return Math.round(subtotal(order.items) * 0.1);
64}
65
66function distanceKm(a, b) {
67 const dLat = (b.lat - a.lat) * 111;
68 const dLng = (b.lng - a.lng) * 111 * Math.cos((a.lat * Math.PI) / 180);
69 return Math.sqrt(dLat * dLat + dLng * dLng);
70}
71
72function nearestRider(riders, address) {
73 let best = null;
74 for (const rider of riders) {
75 if (!rider.online) continue;
76 const d = distanceKm(rider.position, address.position);
77 if (d > MAX_DISTANCE_KM) continue;
78 if (!best || d < best.distance) best = { rider, distance: d };
79 }
80 return best;
81}
82
83function assignRider(order, riders) {
84 const match = nearestRider(riders, order.address);
85 if (!match) return refund(order, "No rider nearby, sorry!");
86 order.riderId = match.rider.id;
87 order.eta = Math.ceil(match.distance * 4) + 10;
88 notify(match.rider.phone, "New order: " + order.id);
89 return order;
90}
91
92function etaMinutes(order, now) {
93 const rush = isLunchRush(now) ? 10 : 0;
94 return order.eta + rush;
95}
96
97function refund(order, reason) {
98 order.status = "refunded";
99 order.refundReason = reason;
100 payments.refund(order.paymentId, totalFor(order));
101 notify(order.customerPhone, "Refunded: " + reason);
102 return order;
103}
104
105function markDelivered(order, now) {
106 order.status = "delivered";
107 order.deliveredAt = now.toISOString();
108 const late = now - new Date(order.placedAt) > order.eta * 60000;
109 if (late) order.lateBy = Math.round((now - new Date(order.placedAt)) / 60000 - order.eta);
110 return order;
111}
112
113function kitchenTicket(order) {
114 const lines = order.items.map((i) => i.qty + " x " + i.name);
115 lines.unshift("ORDER " + order.id);
116 if (order.notes) lines.push("NOTE: " + order.notes);
117 return lines.join("\n");
118}
119
120function printTicket(order) {
121 const ticket = kitchenTicket(order);
122 printer.print(ticket);
123 log("printed", order.id);
124}
125
126// DEAD CODE: the old receipt printer, replaced by email receipts
127function printReceipt(order) {
128 const lines = order.items.map((i) => i.name + " " + formatPrice(i.price * i.qty));
129 lines.push("TOTAL " + formatPrice(totalFor(order)));
130 receiptPrinter.print(lines.join("\n"));
131}
132
133function emailReceipt(order, user) {
134 const body = renderReceipt(order);
135 mailer.send(user.email, "Your Byte Burrito receipt", body);
136}
137
138function menuItem(id) {
139 const item = MENU.find((m) => m.id === id);
140 if (!item) throw new Error("not on the menu: " + id);
141 return item;
142}
143
144function isSpicy(id) {
145 return menuItem(id).spicy === true;
146}
147
148function happyHourPrice(item, now) {
149 const hour = now.getHours();
150 if (hour >= 17 && hour < 19) return Math.round(item.price * 0.8);
151 return item.price;
152}
153
154function priceList(now) {
155 return MENU.map((item) => ({
156 id: item.id,
157 name: item.name,
158 price: formatPrice(happyHourPrice(item, now)),
159 }));
160}
161
162// DEAD CODE: the 2019 "burrito of the month", never shipped
163function burritoOfTheMonth(month) {
164 const picks = ["al-pastor", "veggie", "carnitas", "birria"];
165 return menuItem(picks[month % picks.length]);
166}
167
168function placeOrder(cart, user, now) {
169 const order = { id: newId(), items: cart.items, address: user.address, placedAt: now.toISOString() };
170 const problem = validateOrder(order);
171 if (problem) throw new Error(problem);
172 const fee = deliveryFee(order, distanceKm(KITCHEN, order.address));
173 order.discount = studentDiscount(order, user);
174 order.total = totalFor(order) - order.discount + fee;
175 return order;
176}
177
178function deliveryFee(order, km) {
179 const base = km <= 2 ? 0 : 1.5;
180 const perKm = km > 2 ? (km - 2) * 0.4 : 0;
181 const rush = isLunchRush(new Date()) ? 1 : 0;
182 return Math.round((base + perKm + rush) * 100) / 100;
183}
184
185function newId() {
186 return "BB-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 6);
187}
188
189function log(event, ...details) {
190 console.log("[" + new Date().toISOString() + "] " + event, ...details);
191}
192
193function notify(phone, text) {
194 if (!phone) return;
195 sms.send(phone, text.slice(0, 160));
196}
197
198function saveUser(user) {
199 db.users.put(user.id, user);
200}
201
202function loadUser(id) {
203 return db.users.get(id);
204}
205
206function saveOrder(order) {
207 db.orders.put(order.id, order);
208 log("saved", order.id);
209}
210
211function loadOrder(id) {
212 return db.orders.get(id);
213}
214
215function ordersToday(now) {
216 const day = now.toISOString().slice(0, 10);
217 return db.orders.all().filter((o) => o.placedAt.startsWith(day));
218}
219
220function revenueToday(now) {
221 return ordersToday(now).reduce((sum, o) => sum + o.total, 0);
222}
223
224function busiestHour(now) {
225 const counts = new Array(24).fill(0);
226 for (const o of ordersToday(now)) counts[new Date(o.placedAt).getHours()]++;
227 return counts.indexOf(Math.max(...counts));
228}
229
230// DEAD CODE: the CSV export the accountant asked for, then never used
231function exportCsv(orders) {
232 const rows = orders.map((o) => [o.id, o.total, o.status].join(","));
233 rows.unshift("id,total,status");
234 return rows.join("\n");
235}
236
237function ridersOnline(riders) {
238 return riders.filter((r) => r.online).length;
239}
240
241function riderReport(riders, now) {
242 const online = ridersOnline(riders);
243 const rush = isLunchRush(now);
244 return { online, rush, short: rush && online < 6 };
245}
246
247function weatherDelay(weather) {
248 if (weather === "rain") return 8;
249 if (weather === "snow") return 20;
250 return 0;
251}
252
253function adjustEta(order, weather) {
254 order.eta = order.eta + weatherDelay(weather);
255 return order;
256}
257
258function cancelOrder(order, reason) {
259 if (order.status === "delivered") return order;
260 order.status = "cancelled";
261 return refund(order, reason);
262}
263
264function reorder(previous, user, now) {
265 const cart = { items: previous.items.map((i) => ({ ...i })) };
266 return placeOrder(cart, user, now);
267}
268
269function favourite(user) {
270 const counts = {};
271 for (const o of db.orders.byUser(user.id)) {
272 for (const i of o.items) counts[i.id] = (counts[i.id] || 0) + i.qty;
273 }
274 return Object.keys(counts).sort((a, b) => counts[b] - counts[a])[0];
275}
276
277module.exports = {
278 isOpen,
279 placeOrder,
280 assignRider,
281 markDelivered,
282 printTicket,
283 emailReceipt,
284 priceList,
285 revenueToday,
286 riderReport,
287 cancelOrder,
288 reorder,
289};
NORMALエディタを読み込み中…0.0s行1、列:1
リモコン中:キーはコマンドです打ちかけのコマンドはEscで取り消せます手元にあるのはどっち?カーソルを見て
7ステップ中5
パート3:切る

}で切れ目へ、d}で切る

死んだ関数は、どれも段落です。コメントと中身と、前後の空行。上の空行に立ってd}を押せば、次の切れ目まで切れます。関数は消え、空行が1つ残ります。

切るたびにファイルは短くなり、下の行番号は変わります。}は気にしません。数えるのは切れ目です。

1function etaMinutes(order, now) {
1 const rush = isLunchRush(now) ? 10 : 0;
2 return order.eta + rush;
3}
4
5// DEAD CODE: the pager integration, before the riders had phones
6function pageRider(rider, order) {
7 const message = "ORDER " + order.id + " " + order.address.street;
8 pager.send(rider.pagerId, message.slice(0, 80));
9 return true;
10}
11
12function refund(order, reason) {
13 order.status = "refunded";
14 order.refundReason = reason;
15 payments.refund(order.paymentId, totalFor(order));
16 notify(order.customerPhone, "Refunded: " + reason);
17 return order;
18}
NORMALエディタを読み込み中…行1、列:1
ボス戦

ステージ2 · 死んだものを切る

4時40分。死んだ関数3つを、上から順に。古いレシートプリンター、今月のブリトー、CSVの書き出し。コメントごと切り、周りの関数の間には空行を1つ残します。道順は自由です。

答えを全部見る
練習問題を開いています…
Pro
このレッスンの続きはProで

どのレッスンも最初の練習問題は無料。Proなら残りも全トラックも、ずっと使えます。

Proを見る

この練習問題にはキーボードが必要です。 このまま読み進めて、パソコンで試してください。

整備リスト死んだコードのリストには、まだ印のない項目が3つ。金曜日の4時です。上の練習問題をクリアすると見られます。
7ステップ中6
思い出す練習

今週は、どのキー?

どのキー?
欄によると、行きたい行は12行下。そこへ。
ここまでスクロールして、キーを押す下の3キーをタップ
7ステップ中7
おまけの練習

おまけの練習やらなくてもOK

このチャプターはクリアです。ここからは好きなだけ続けられます。飽きたらそこでおしまい。

エンドレス

長い本

legacy.jsのどこでも、縦の動きなら何でも、終わりなく。

練習問題を開いています…
Pro
このレッスンの続きはProで

どのレッスンも最初の練習問題は無料。Proなら残りも全トラックも、ずっと使えます。

Proを見る

この練習問題にはキーボードが必要です。 このまま読み進めて、パソコンで試してください。