Skip to content

Instantly share code, notes, and snippets.

@marconvcm
Created September 5, 2019 02:24
Show Gist options
  • Save marconvcm/f4a9bcb927b46efe7fe3b573bc7e6496 to your computer and use it in GitHub Desktop.
Save marconvcm/f4a9bcb927b46efe7fe3b573bc7e6496 to your computer and use it in GitHub Desktop.
Hacker Rank
// https://www.hackerrank.com/challenges/bon-appetit/problem?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=7-day-campaign&isFullScreen=true
const assert = require('assert');
function bonAppetit(bill, k, b) {
let amount = calculateBill(removePosition(bill, k));
let change = b - amount;
console.log(change == 0 ? 'Bon Appetit' : change);
}
function removePosition(arr, i) {
const arr2 = [...arr];
arr2.splice(i, 1);
return [...arr2];
}
function calculateBill(arr) {
let sum = arr.reduce((acc, next) => acc + next, 0);
return sum / arr.length;
}
describe('bill', () => {
it('bonAppetit', () => {
const bill = [2, 4, 6];
const k = 2;
const b = 5;
const expected = 2;
const result = bonAppetit(bill, k, b);
assert.equal(result, expected);
});
it('bonAppetit2', () => {
const bill = [2, 4, 6];
const k = 2;
const b = 3;
const expected = 'Bon Appetit';
const result = bonAppetit(bill, k, b);
assert.equal(result, expected);
});
it('calculateBill', () => {
const input = [6, 10];
const expected = 8;
const result = calculateBill(input);
assert.equal(result, expected);
});
it('removePosition', () => {
const input = [1, 2, 3, 4];
const expt = [1, 3, 4];
const result = removePosition(input, 1);
assert.deepEqual(result, expt);
assert.notDeepEqual(result, input);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment