Created
June 28, 2022 14:39
-
-
Save vpellegrino/186d075b86262cc6cbdb1967afcf33e4 to your computer and use it in GitHub Desktop.
The city provides its citizens with a Walk Generating App on their phones -- every time you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so cr…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* production code | |
*/ | |
const backFromWalkInTime = (steps) => { | |
const stepsUp = steps.filter((step) => step === "n").length; | |
const stepsDown = steps.filter((step) => step === "s").length; | |
const stepsLeft = steps.filter((step) => step === "e").length; | |
const stepsRight = steps.filter((step) => step === "w").length; | |
return ( | |
steps.length === 10 && stepsUp === stepsDown && stepsLeft === stepsRight | |
); | |
}; | |
/** | |
* unit tests | |
*/ | |
mocha.setup("bdd"); | |
const assert = chai.assert; | |
describe("given an array containing coordinates, checking if I will be back from walk in time", () => { | |
describe("when the sequence of steps is balanced", () => { | |
it("should return true", function () { | |
assert.equal( | |
backFromWalkInTime(["n", "e", "w", "s", "n", "w", "w", "e", "e", "s"]), | |
true | |
); | |
}); | |
}); | |
describe("when the sequence of steps is NOT balanced", () => { | |
it("should return false", function () { | |
assert.equal( | |
backFromWalkInTime(["s", "e", "w", "s", "n", "w", "w", "e", "e", "s"]), | |
false | |
); | |
}); | |
}); | |
describe("when it has less than 10 steps", () => { | |
it("should return false", function () { | |
assert.equal(backFromWalkInTime(["n", "e", "w"]), false); | |
}); | |
}); | |
describe("when it is empty", () => { | |
it("should return false", function () { | |
assert.equal(backFromWalkInTime([]), false); | |
}); | |
}); | |
}); | |
mocha.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment