Created
December 4, 2018 21:55
-
-
Save bradtaniguchi/fae727dc541d655501a2d58e1a143bea to your computer and use it in GitHub Desktop.
A programming challenge
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
///https://www.hackerrank.com/challenges/new-year-chaos/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays | |
const assert = require('assert'); | |
const throwErr = num => { | |
throw new Error(num); | |
}; | |
const minBribes = arr => | |
arr.reduce( | |
(bribes, num, index) => { | |
if (index + 1 < num - 2) { | |
throwErr(num); | |
} | |
console.log(bribes, num, index); | |
return bribes + (num - (index + 1)); | |
}, | |
// index + 1 < num - 2 | |
// ? throwErr(num) | |
// : bribes + (num - (index + 1)), | |
0 | |
); | |
// assert.equal(minBribes([2, 1]), 1); | |
assert.equal(minBribes([3, 2, 1]), 2); | |
// assert.equal(minBribes([2, 1, 5, 3, 4]), 3); | |
// assert.equal(minBribes([]), 0); | |
// assert.equal(minBribes([1, 2, 3]), 0); | |
// assert.equal(minBribes([2, 5, 1, 3, 4]), 'Too chaotic'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment