question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add extra test: Prevent assigning value to `balance` property in "Bank Account" exercise

See original GitHub issue

The “Bank Account” exercise should have a test case that raises ValueError when directly assigning value to balance property.

Below code(many user submissions have similar code) passes all the current test cases:

class BankAccount {
  constructor() {
    this.isActive = false
    this._balance
  }

  open() {
    if (this.isActive) throw new ValueError()
    this.isActive = true
    this.balance = 0
  }

  close() {
    if (this.isActive) this.isActive = false
    else throw new ValueError()
  }

  deposit(amt) {
    if (amt < 0) throw new ValueError()
    this.balance += amt
  }

  withdraw(amt) {
    if (amt < 0 || amt > this.balance) throw new ValueError()
    this.balance -= amt
  }

  get balance() {
    if (!this.isActive) throw new ValueError()
    return this._balance
  }

  set balance(amt) {
    this._balance = amt
  }
}

class ValueError extends Error {
  constructor() {
    super('Bank account error');
  }
}

There’s no test case that raises error on doing

let ba = new BankAccount()
ba.balance = 100 // Expected ValueError

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
kshivakumarcommented, Nov 19, 2021

That’s a good idea, raising a PR.

0reactions
junedevcommented, Nov 18, 2021

We circled back to the no setter vs. setter with ValueError decision. Let me propose a compromise before I ask a senior maintainer to make a call here.

What about the new test just checks that an error is thrown when trying to assign a balance but does not require a specific error type, just any Error. That way no setter and a setter that throws an error would both pass the tests which makes sense since both are valid solutions to the problem. WDYT?

Read more comments on GitHub >

github_iconTop Results From Across the Web

3.5 Account Class with a Balance; Floating-Point Numbers
We now declare an Account class that maintains the balance of a bank account in addition to the name. Most account balances are...
Read more >
Structure and Interpretation of Computer Programs, 2e: 3.1
Observe that the expression (withdraw 25) , evaluated twice, yields different values. This is a new kind of behavior for a procedure.
Read more >
Computer Science | C# | Assignment: Exercise 6 Windows App
Define the following fields and properties: 1) A field whose purpose is to hold the account balance. This field—is NOT a property—and will...
Read more >
creating a basic bank system, keyword 'this' in javascript
Ensure that the accounts cannot have negative values. Write a 'transfer' on the bank that allows you to transfer amounts between two accounts. ......
Read more >
Question 1a. Let us design a class bankAccount. A bank ...
name of the owner (ii) account number (iii) current balance, and (iv) deposit money in the account. [15] class bankAccount{.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found