Add extra test: Prevent assigning value to `balance` property in "Bank Account" exercise
See original GitHub issueThe “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:
- Created 2 years ago
- Comments:15 (15 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s a good idea, raising a PR.
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?