Skip to content

Instantly share code, notes, and snippets.

@bradtaniguchi
Created December 4, 2018 21:55
Show Gist options
  • Save bradtaniguchi/fae727dc541d655501a2d58e1a143bea to your computer and use it in GitHub Desktop.
Save bradtaniguchi/fae727dc541d655501a2d58e1a143bea to your computer and use it in GitHub Desktop.
A programming challenge
///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