-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathBugs Life.js
23 lines (17 loc) · 1.07 KB
/
Bugs Life.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Description:
A bug lives in a world which is a cuboid and has to walk from one corner of the cuboid to the opposite corner (see the picture below).
Google 'Cuboid Space Diagonal'
Your task: Define a function which takes 3 arguments: the length a , width b, and height c of the bug's "world", and finds the shortest distance needed to walk from start to finish. The dimensions will be positive numbers.
Note: The bug cannot fly and has to maintain contact with a surface at all times but can walk up walls.
Example: shortestDistance(1,2,3) === 4.242640687119285.
Hint 1: Consider how many different routes can be made to get from start to finish. If stuck for where to start, click here for general guidance.
Hint 2: Consider using paper and opening up the net of a cuboid (and think if there are multiple ways this can be viewed) and play around with it to find the shortest length.
*/
function shortestDistance(a, b, c) {
return Math.min(
Math.sqrt(Math.pow(a,2) + (b+c) * (b+c)),
Math.sqrt(Math.pow(b,2) + (a+c) * (a+c)),
Math.sqrt(Math.pow(c,2) + (a+b) * (a+b))
);
}