xoxama

Bank Account

May 5th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Create a bank account object using revealing module pattern. The object should have the following properties:
  2. balance - number
  3. 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
  4. 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.
  5. getBalance - function that will return the current value of the balance. Research and use a getter to do it. */
  6.  
  7. const bankAccount = () => {
  8.   let balance = 0;
  9.   let currBalance = 0;
  10.  
  11.   const deposit = (num) => {
  12.     if (num > 0 && !isNaN(num)) {
  13.       balance += num;
  14.       return balance;
  15.     } else {
  16.       return `Invalid operation. Please try again!`;
  17.     }
  18.   }
  19.  
  20.   const withdraw = (num) => {
  21.     if (num > balance || isNaN(num)) {
  22.       return `You cannot perform this operation!`;
  23.     } else {
  24.       balance -= num;
  25.       return balance;
  26.     }
  27.   }
  28.  
  29.   const getBalance = () => {
  30.     if (balance === 0) {
  31.       return `Your balance shows that you don't have money in your account!`;
  32.    } else {
  33.      return balance;
  34.    }
  35.  }
  36.  
  37.  return {
  38.    deposit,
  39.    withdraw,
  40.    getBalance
  41.  }
  42. }
  43.  
  44. let myAccount = bankAccount();
  45.  
  46. console.log(myAccount.deposit(1000));
  47. console.log(myAccount.withdraw(11000));
  48. console.log(myAccount.getBalance());
Add Comment
Please, Sign In to add comment