Skip to content

Instantly share code, notes, and snippets.

@ericabell
ericabell / diff.js
Last active September 13, 2017 21:58
Difference in paths
// Given two absolute paths on a file system, write a function to determine the difference between them.
// For example, `/a/b/c` and `/a/d` should yield `../../d`.
// `/a/b/c` and `/e/f` => `../../../e/f`
// `/` and `/a/b/c` => `/a/b/c`
// `/a/b/c` and `/` => `../../../`
function difference(path1, path2) {
// break the paths into lists and discard the first empty element