Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Create a bank account object using revealing module pattern. The object should have the following properties:
- balance - number
- deposit - function that will take one number as a parameter and will add the number to the ballance. Check against invalid numbers and throw an Error
- withdraw - function that will take one number as a parameter and will subtract the number from the ballance. Check against invalid numbers and throw an Error. Think about what else can go wrong here and guard against it.
- getBalance - function that will return the current value of the balance. Research and use a getter to do it. */
- const bankAccount = () => {
- let balance = 0;
- let currBalance = 0;
- const deposit = (num) => {
- if (num > 0 && !isNaN(num)) {
- balance += num;
- return balance;
- } else {
- return `Invalid operation. Please try again!`;
- }
- }
- const withdraw = (num) => {
- if (num > balance || isNaN(num)) {
- return `You cannot perform this operation!`;
- } else {
- balance -= num;
- return balance;
- }
- }
- const getBalance = () => {
- if (balance === 0) {
- return `Your balance shows that you don't have money in your account!`;
- } else {
- return balance;
- }
- }
- return {
- deposit,
- withdraw,
- getBalance
- }
- }
- let myAccount = bankAccount();
- console.log(myAccount.deposit(1000));
- console.log(myAccount.withdraw(11000));
- console.log(myAccount.getBalance());
Add Comment
Please, Sign In to add comment