Last active
September 24, 2018 21:23
-
-
Save Anna-Myzukina/2571e6e96b9c5c897a8ad9305815b995 to your computer and use it in GitHub Desktop.
js-best-practices
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
//Exercise 2 Separation of Concerns 1 | |
//Separation of Concerns Part 1 | |
/*take the 4 methods and any variables that relate to | |
balance management and move them to balanceManager.js. Then, back in | |
vendingMachine.js, change the method of calling those functions from this | |
to our new balanceManager | |
*/ | |
var balance = 0; | |
module.exports = { | |
canAfford: function(amount){ | |
if(!this.isValidAmount(amount)){ | |
errorMessage = "Invalid Input"; | |
} | |
if(errorMessage){ | |
throw new Error(errorMessage); | |
} | |
return amount <= balance; | |
}, | |
decreaseBalance: function(amount){ | |
// This method decreases the balance of the vending machine. If the balance amount is not | |
// enough to cover the purchase, the method throws an error. | |
var errorMessage; | |
if(!this.canAfford(amount)){ | |
errorMessage = 'Insufficient balance'; | |
} | |
if(errorMessage){ | |
throw new Error(errorMessage); | |
} | |
balance -= amount; | |
}, | |
getBalance: function(){ | |
return balance; | |
}, | |
increaseBalance: function(amount){ | |
balance += amount; | |
}, | |
}; |
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
//Exercise 3 Separation of Concerns Part 2 | |
/*We have already separated out all of the balance related methods, now we | |
will look at the change related ones. Take the method that relates to | |
handling change and move it into changeHandler.js | |
*/ | |
module.exports = { | |
getAmount: function(coinType) { | |
// COINS: | |
// [p]enny | |
// [n]ickel | |
// [d]ime | |
// [q]uarter | |
switch(coinType){ | |
case 'p': return 1; | |
case 'n': return 5; | |
case 'd': return 10; | |
case 'q': return 25; | |
default: throw new Error('Unrecognized coin ' + coinType); | |
} | |
} | |
}; |
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
//Exercise 4 Separation of Concerns 3 | |
/*Take the two methods that relate to products and move them into | |
productInventory.js | |
*/ | |
var products = [ | |
{ | |
name: 'Skittles', | |
price: 85, | |
id: 'A1' | |
} | |
]; | |
module.exports = { | |
getProducts: function () { | |
return products; | |
}, | |
getProduct: function (productId) { | |
var product = products.find(function (p) { return p.id === productId; }); | |
return product; | |
} | |
}; |
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
//Exercise 5 Getting Rid of Switch Statements | |
/*you should refactor the getAmount() method in | |
changeHandler.js to completely remove the switch method while retaining | |
its functionality. It doesn't matter what alternative you choose, but the | |
method should not use a switch statement in any way | |
*/ | |
// COINS: | |
// [p]enny | |
// [n]ickel | |
// [d]ime | |
// [q]uarter | |
var coins = { | |
p: 1, | |
n: 5, | |
d: 10, | |
q: 25 | |
}; | |
module.exports = { | |
getAmount: function(coinType) { | |
if (coins.hasOwnProperty(coinType)) { | |
return coins[coinType]; | |
} | |
throw new Error('Unrecognized coin ' + coinType); | |
} | |
}; |
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
//Exercise 6 | |
/*The tests in this exercise are provided, but they will drive the design of | |
the function you will implement, convertToChange(). convertToChange() | |
should take in a single integer number in cents (e.g. $0.89 = 89) and | |
return an array of the coins that would add up to this number. Keep in | |
mind that the array of coins should be in the highest order in terms of | |
coin value. For example, if 26 is the parameter, you should return 1 penny | |
and 1 quarter in the array, even though you could technically return | |
several other combinations of coins (e.g. 2 dimes, 1 nickel, 1 penny) | |
*/ | |
// COINS: | |
// [p]enny | |
// [n]ickel | |
// [d]ime | |
// [q]uarter | |
var coins = { | |
p: 1, | |
n: 5, | |
d: 10, | |
q: 25 | |
}; | |
var coinsByAmount = ['q', 'd', 'n', 'p']; | |
module.exports = { | |
convertToChange: function(amount) { | |
var change = []; | |
for(var i in coinsByAmount){ | |
var coinType = coinsByAmount[i]; | |
var coinValue = coins[coinType]; | |
while(amount >= coinValue){ | |
change.push(coinType); | |
amount -= coinValue; | |
} | |
} | |
return change; | |
} | |
}; |
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
//Exercise 7 Fix Your Teammates' Mistakes | |
var balance = 0; | |
module.exports = { | |
increaseBalance: function(amount){ | |
balance += amount; | |
}, | |
getBalance: function(){ | |
return balance; | |
}, | |
canAfford: function(amount){ | |
var errorMessage; | |
if(!this.isValidAmount(amount)){ | |
errorMessage = 'Invalid Input'; | |
} | |
if(errorMessage){ | |
throw new Error(errorMessage); | |
} | |
return amount <= balance; | |
}, | |
decreaseBalance: function(amount){ | |
if(!this.canAfford(amount)){ | |
var errorMessage = 'Insufficient balance'; | |
} | |
if(errorMessage){ | |
throw new Error(errorMessage); | |
} | |
balance -= amount; | |
}, | |
isValidAmount: function(amount){ | |
if(!amount){ | |
return false; | |
} else { | |
return true; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment