Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created October 26, 2025 17:47
Show Gist options
  • Select an option

  • Save tatsuyax25/808fd796fba46f0bd10eb11b993b2b55 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/808fd796fba46f0bd10eb11b993b2b55 to your computer and use it in GitHub Desktop.
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-inde
/**
* Bank constructor initializes the bank with account balances.
* @param {number[]} balance - Array of initial balances for each account (1-indexed).
*/
var Bank = function(balance) {
// Create an array of accounts with an extra slot at index 0 (unused).
// This allows 1-based indexing for accounts.
this.accounts = Array.from({ length: balance.length + 1 }, (_, i) => balance[i - 1] ?? 0);
// Store the total number of accounts including the unused 0th index.
this.size = balance.length + 1;
};
/**
* Transfers money from one account to another.
* @param {number} account1 - Source account number (1-indexed).
* @param {number} account2 - Destination account number (1-indexed).
* @param {number} money - Amount to transfer.
* @return {boolean} - Returns true if transfer is successful, false otherwise.
*/
Bank.prototype.transfer = function(account1, account2, money) {
// Validate account numbers are within bounds.
if (account1 < 1 || account1 >= this.size || account2 < 1 || account2 >= this.size) return false;
// Check if the source account has enough funds.
if (this.accounts[account1] < money) return false;
// Perform the transfer.
this.accounts[account1] -= money;
this.accounts[account2] += money;
return true;
};
/**
* Deposits money into an account.
* @param {number} account - Account number (1-indexed).
* @param {number} money - Amount to deposit.
* @return {boolean} - Returns true if deposit is successful, false otherwise.
*/
Bank.prototype.deposit = function(account, money) {
// Validate account number.
if (account < 1 || account >= this.size) return false;
// Add money to the account.
this.accounts[account] += money;
return true;
};
/**
* Withdraws money from an account.
* @param {number} account - Account number (1-indexed).
* @param {number} money - Amount to withdraw.
* @return {boolean} - Returns true if withdrawal is successful, false otherwise.
*/
Bank.prototype.withdraw = function(account, money) {
// Validate account number.
if (account < 1 || account >= this.size) return false;
// Check if the account has enough funds.
if (this.accounts[account] < money) return false;
// Deduct money from the account.
this.accounts[account] -= money;
return true;
};
/**
* Your Bank object will be instantiated and called as such:
* var obj = new Bank(balance)
* var param_1 = obj.transfer(account1,account2,money)
* var param_2 = obj.deposit(account,money)
* var param_3 = obj.withdraw(account,money)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment