Created
June 12, 2021 20:14
-
-
Save marclerodrigues/b479708ec373cef7faf0996628c8c850 to your computer and use it in GitHub Desktop.
Bon Appetit Challenge
This file contains hidden or 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
| 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); | |
| } | |
| } |
This file contains hidden or 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
| 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