patternjavascriptMinor
ECMAScript 6 budget calculator
Viewed 0 times
ecmascriptbudgetcalculator
Problem
I never get to use ECMA6 in production/work, so I am hoping you all can help me write better code by reviewing my finished sample:
``
format
``
(function() {
'use strict';
class BudgCalc {
constructor(personFullName, yearlyAfterTaxIncome) {
this.name = personFullName;
this.income = this.dollarsToCents(yearlyAfterTaxIncome);
this.initialAfterTaxIncome = this.income;
this.expenses = {
daily: [],
weekly: [],
monthly: [],
yearly: []
}
// To be calculated at runtime
this.incomeBreakdown = {
_startingIncome: this.centsToDollars(this.initialAfterTaxIncome), // _ is so its at the top of the object in the console only not because its private:)
daily: 0,
weekly: 0,
monthly: 0,
yearly: 0
}
// Constants storing occurances per year
this.dateData = {
DAILY: 365,
WEEKLY: 52,
MONTHLY: 12,
YEARLY: 1
}
this.setAmountsAvailableByTimePeriod();
return this;
}
// dollars string to cents int
dollarsToCents(curString) {
var dolCentArr = curString.replace('$', '').replace(',', '').split('.'),
dollarToCent = dolCentArr[0] * 100,
origCents = dolCentArr[1] || 0, // For exact dollar amounts
cents;
if (origCents.length > 2) origCents = [origCents[0], origCents[1]]; // Truncate anything larger than .00
cents = (parseInt(dollarToCent) + parseInt(origCents)); // Avoid type coersion here!
return Math.round(cents);
}
// Turns dollars int to formatted USD currency string
centsToDollars(centsInt) {
var centsStr = $${(parseInt(centsInt) / 100)}`,format
Solution
Your code looks pretty good but some suggestions are:
Use
If you're going to use ES6, you should probably use
Use
If your function is not referencing
to...
this may mean you'll have to change parts of your code but it allows access to these functions without having to go through
Try to make conditionals clear at first glance
When I see:
It takes me a moment to figure out what this means. Rewriting this makes this a lot more clear:
Use a radix on
You're using
if your "stringified" number is guaranteed to be an integer, you can use any of the following:
Try to simplify code
When I first saw this, it took me a moment to see you're trying to get the first to digits.
Also, note this returns an array which you are passing into
Try to use regex more
Right here you have a couple of regexes:
instead you could simplify this to:
%%CODEBLOCK_9%%
this will also be global and will remove all occurrences.
String templates
You're using string templates which is awesome but you can still make some more things string templates:
%%CODEBLOCK_10%%
can become...
%%CODEBLOCK_11%%
Use
letIf you're going to use ES6, you should probably use
let. let generally helps avoid problems with scoping. Try to also use const for constants.Use
staticIf your function is not referencing
this you should probably use static.dollarsToCents(curString) {to...
static dollarsToCents(curString) {this may mean you'll have to change parts of your code but it allows access to these functions without having to go through
prototype or create an instance of the class.Try to make conditionals clear at first glance
When I see:
!centsMember.lengthIt takes me a moment to figure out what this means. Rewriting this makes this a lot more clear:
centsMember.length === 0Use a radix on
parseInt!You're using
parseInt without a radix. On some browsers, sometimes the browser will assume the wrong base which will cause problems, instead use:parseInt(n, 10)if your "stringified" number is guaranteed to be an integer, you can use any of the following:
Number(n)
+nTry to simplify code
When I first saw this, it took me a moment to see you're trying to get the first to digits.
if (origCents.length > 2) origCents = [origCents[0], origCents[1]]Also, note this returns an array which you are passing into
parseInt which will cause problems. Instead try something like:origCents = origCents.substr(0, 2);Try to use regex more
Right here you have a couple of regexes:
.replace('
instead you could simplify this to:
.replace(/[$,]/g, '');
this will also be global and will remove all occurrences.
String templates
You're using string templates which is awesome but you can still make some more things string templates:
(6450 * 24).toString() + '.00'
can become...
`${6450 * 24}.00`
, '').replace(',', '')instead you could simplify this to:
%%CODEBLOCK_9%%
this will also be global and will remove all occurrences.
String templates
You're using string templates which is awesome but you can still make some more things string templates:
%%CODEBLOCK_10%%
can become...
%%CODEBLOCK_11%%
Code Snippets
dollarsToCents(curString) {static dollarsToCents(curString) {!centsMember.lengthcentsMember.length === 0parseInt(n, 10)Context
StackExchange Code Review Q#122958, answer score: 3
Revisions (0)
No revisions yet.