Challenge: Transit planner

Run Settings
LanguageJavaScript
Language Version
Run Command
// Here's the explanation of the challenge -- when you're done reading it, // click on `main.js` above to get started with the challenge! // Getting around in a big city is no easy task! With so many options to get // from point A to point B, it's easy to get confused. Fortunately, we can use // code to figure out the best way to travel. // ## Description // In this challenge, you will write a `shortestTrip` function that receives a // map (object) as an argument. Each key in the map is the name of a mode of // transit between two places, and its value is an array containing the // departure and arrival times for that mode of transit, represented as // strings. The map represents the different modes of transit available // between two points in a city. // For example, if the trip can be done by taking the train, a bus, or // walking, your function could be invoked like: // shortestTrip({ // bus: ["09:15", "09:36"], // train: ["09:30", "09:42"], // walk: ["09:02", "09:40"] // }) // Your function should return the name of the transit method in which the // trip takes **the least time to complete** -- _not_ the transit method that // arrives earlier, but the one in which you spend the least time travelling. // The list of modes of transit will always contain at least one mode, and //t he time will always be represented in the `hh:mm` format, using two // digits for the hours and for the minutes. // The times will never represent starting and ending on different days, // meaning the travel times will never cross midnight. The departure time will // always be earlier than the arrival time. No two trips will take the same // amount of time.
/* Check out README.md for instructions! */ function shortestTrip(modes) { /* Your code goes here! */ /* Press [▶ Run] to see your code in action! */ /* You'll see the output of the tests below the button */ } /* Test runner (do not modify this line!) */ const expect = require("./expect")(shortestTrip) /* Test cases (remember to add more!) */ // i walk a lonely road... expect("walk", { "walk": ["09:10", "10:47"] }) // picks the shortest travel time expect("bus", { "walk": ["08:13", "08:53"], "bus": ["08:45", "09:02"] }) // your test case here! // expect(expectedMode, modes)
/* Test runner (do not modify this!) */ module.exports = (run) => function expect(expected, ...inputs) { const stringify = (value) => { const serialised = JSON.stringify(value, undefined, 2) || "undefined" const lines = serialised.split('\n') for (i = 1; i < lines.length; i++) { lines[i] = "\t" + lines[i] } return lines.join('\n') } const serialisedInputs = inputs .map(stringify) .join(", ") const actual = run(...inputs) if (expected !== actual) { console.log( ` ❌ ${run.name}(${serialisedInputs})\n\n` + `\texpected: ${stringify(expected)}\n` + `\tbut instead got: ${stringify(actual)}\n\n` ) } else { console.log( ` ✅ ${run.name}(${serialisedInputs}) === ${stringify(actual)}\n` ) } }
Editor Settings
Theme
Key bindings
Full width
Lines