...

/

Step 3: Account Module

Step 3: Account Module

We'll cover the following...

To process transactions, we need an Account class. We will use the class syntax of ES6 to write the code. A short explanation will follow the example. Before reading the explanation, try to figure out what the code does.

Press + to interact
import { sortBy, first } from 'underscore';
class Account {
constructor() {
this.transactions = [];
}
getTopTransactions() {
var getSortKey = transaction =>
-Math.abs( transaction.amount );
var sortedTransactions = sortBy(
this.transactions,
getSortKey
);
return first( sortedTransactions, 3 );
}
deposit( amount, date ) {
this.transactions.push({
amount : amount,
date : date
});
}
withdraw( amount, date ) {
this.transactions.push({
amount : -amount,
date : date
});
}
};
export default Account;

Save the above code in src/Account.js. ...