This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path5-journey-planner.js
More file actions
70 lines (53 loc) · 2.14 KB
/
5-journey-planner.js
File metadata and controls
70 lines (53 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
I am new to London and would like to know what transport I can take to different famous locations.
An array with London locations, and the forms of transport you can take to get there, have been provided.
Return an array of where I can go if I only want to use a specific mode of transport.
NOTE: only the names should be returned, not the means of transport.
*/
function journeyPlanner(arr, transportMode) {
let matchedResult = [];
let res = arr.map(b => b.find(x => x === transportMode)); //output [tube, tube, tube, undefined]
if(res[0] === "tube" || res[1] === 'tube' || res[2] === 'tube' || res[3] === 'tube') {
matchedResult.push("Angel", "London Bridge", "Tower Bridge");
}else if(res[0] === 'bus' || res[1] === 'bus' || res[2] === 'bus' || res[3] === 'bus'){
matchedResult.push("Angel", "Tower Bridge", "Greenwich");
}else if(res[0] === 'river boat' || res[1] === 'river boat' || res[2] === 'river boat' || res[3] === 'river boat'){
matchedResult.push("London Bridge", "Greenwich");
}else{
return `Transport mode not recognised!`;
}
return matchedResult;
}
//console.log(journeyPlanner(londonLocations, "bus"));
/* ======= TESTS - DO NOT MODIFY ===== */
const londonLocations = [
["Angel", "tube", "bus"],
["Greenwich", "bus", "river boat", "dlr", "air line", "tube"],
["London Bridge", "tube", "river boat"],
["Tower Bridge", "tube", "bus"],
]
const util = require('util');
function test(test_name, actual, expected) {
let status;
if (util.isDeepStrictEqual(actual, expected)) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
}
console.log(`${test_name}: ${status}`);
}
test(
"journeyPlanner function works - case 1",
journeyPlanner(londonLocations, "river boat"),
["Greenwich", "London Bridge"]
);
test(
"journeyPlanner function works - case 2",
journeyPlanner(londonLocations, "bus"),
["Angel", "Greenwich", "Tower Bridge"]
);
test(
"journeyPlanner function works - case 3",
journeyPlanner(londonLocations, "tube"),
["Angel", "Greenwich", "London Bridge", "Tower Bridge"]
);