Skip to content

Instantly share code, notes, and snippets.

@pads
Created July 11, 2020 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pads/cbd98fae983f46726d8f7885350bd591 to your computer and use it in GitHub Desktop.
Save pads/cbd98fae983f46726d8f7885350bd591 to your computer and use it in GitHub Desktop.
// https://github.com/CodeYourFuture/JavaScript-Core-2-Homework/blob/master/Week-1/InClass/D-methods/exercise-5.js
/*
A coffee machine is defined below.
One can buy three different coffees.
Complete the methods "insertMoney" and "getCoffee" to match the expected result.
insertMoney takes an amount in parameter to add money in the coffee machine.
getCoffee takes a coffee type in parameter and dispends the selected coffee
only if the inserted amount is greater or equal than the price of the coffee!
*/
let coffeeMachine = {
brand: "Super Coffee",
prices: {
cappuccino: 2.40,
blackCoffee: 1.50,
flatWhite: 3.00
},
insertedAmount: 0,
insertMoney: function (amount) {
this.insertedAmount = amount;
},
getCoffee: function (coffee) {
if (this.insertedAmount >= this.prices[coffee]) {
return `Please take your ${coffee}`;
} else {
return `Sorry you don't have enough money for a ${coffee}`;
}
}
};
/*
DO NOT EDIT ANYTHING BELOW THIS LINE
*/
coffeeMachine.insertMoney(2.40);
console.log(`Expected result: 'Please take your cappuccino'. Actual result: ${coffeeMachine.getCoffee('cappuccino')}`);
coffeeMachine.insertMoney(1.50);
console.log(`Expected result: 'Please take your blackCoffee'. Actual result: ${coffeeMachine.getCoffee('blackCoffee')}`);
coffeeMachine.insertMoney(4.00);
console.log(`Expected result: 'Please take your flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`);
coffeeMachine.insertMoney(2.40);
console.log(`Expected result: 'Sorry you don't have enough money for a flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment