Skip to content

Instantly share code, notes, and snippets.

@marclerodrigues
Created June 12, 2021 20:14
Show Gist options
  • Select an option

  • Save marclerodrigues/b479708ec373cef7faf0996628c8c850 to your computer and use it in GitHub Desktop.

Select an option

Save marclerodrigues/b479708ec373cef7faf0996628c8c850 to your computer and use it in GitHub Desktop.
Bon Appetit Challenge
function bonAppetit(bill, k, b) {
// Write your code here
let total = 0;
for(let i = 0; i < bill.length; i++) {
if (i !== k) {
total += bill[i];
}
}
const splittedBill = total / 2;
if (splittedBill === b) {
console.log("Bon Appetit");
} else {
console.log(b - splittedBill);
}
}
function bonAppetit(bill, k, b) {
// Write your code here
const consumedByBoth = bill.filter((value, index) => index !== k);
const splittedBill = consumedByBoth.reduce((acc, value) => acc + value) / 2;
if (splittedBill === b) {
console.log("Bon Appetit");
} else {
console.log(b - splittedBill);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment