Skip to main content
Code Review

Return to Question

added 16 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

The full code can be seen here - http://codepen.io/liam-ryan/pen/GgOpvK?editors=001

My intention is to create a nice DRY method ( add add) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here, can. Can anyone help me out?

PS: the intention is to be able to specify different objects with add, so if I had a label object called food'food' with a credit property I could create a function called addFoodCredit(amount)addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food')add( amount, 'credit', 'labels', 'food').

The full code can be seen here.

The full code can be seen here - http://codepen.io/liam-ryan/pen/GgOpvK?editors=001

My intention is to create a nice DRY method ( add ) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here, can anyone help me out?

PS: the intention is to be able to specify different objects with add, so if I had a label object called food with a credit property I could create a function called addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food').

My intention is to create a nice DRY method (add) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here. Can anyone help me out?

PS: the intention is to be able to specify different objects with add, so if I had a label object called 'food' with a credit property I could create a function called addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food').

The full code can be seen here.

Post Reopened by Brythan, Community Bot, Marc-Andre, rolfl
added full code
Source Link
LiamRyan
  • 313
  • 2
  • 9

See codepen to see an example

functionvar totalsObjectdateObject = function() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount, itemkey ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 console.log(objectReference, objectKey);
 console.log(totals.totalDebit)
 };
 this.addCredit = function addCredit( amount ) {
 add( amount, 'totalCredit' );
 };
 this.addDebit = function addDebit (amount) {
 add( amount, 'totalDebit');
 };
 this.addLabel = function ( label, amount, type ) {
 var labelName = label + '-' + type;
 if ( ! totals.labels.labelName ) {
 totals.labels[labelName] = 0;
 }
 
 add(amount, labelName, 'labels')
 }
 this.print = function() {console.log(totals)};

};
var x = new dateObject();
x.addCredit(15);
x.addCredit(21);
x.addDebit(12.42);
x.addDebit(122);
x.addLabel('food', 20, 'dr')
x.print();

See codepen to see an example

function totalsObject() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount, itemkey ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 };
 this.addDebit = function (amount) {
 add( amount, 'totalDebit')
 }
 ....
}
var dateObject = function() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 console.log(objectReference, objectKey);
 console.log(totals.totalDebit)
 };
 this.addCredit = function addCredit( amount ) {
 add( amount, 'totalCredit' );
 };
 this.addDebit = function addDebit (amount) {
 add( amount, 'totalDebit');
 };
 this.addLabel = function ( label, amount, type ) {
 var labelName = label + '-' + type;
 if ( ! totals.labels.labelName ) {
 totals.labels[labelName] = 0;
 }
 
 add(amount, labelName, 'labels')
 }
 this.print = function() {console.log(totals)};

};
var x = new dateObject();
x.addCredit(15);
x.addCredit(21);
x.addDebit(12.42);
x.addDebit(122);
x.addLabel('food', 20, 'dr')
x.print();
Improved title, added parsing tag
Source Link
Phrancis
  • 20.5k
  • 6
  • 69
  • 155

What is wrong with this javascript code? Method for parsing floats and adding totals

The full code can be seen here - http://codepen.io/liam-ryan/pen/GgOpvK?editors=001

My intention is to create a nice DRY method ( add ) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here, can anyone help me out?

PS: the intention is to be able to specify different objects with add, so if I had a label object called food with a credit property I could create a function called addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food').

See codepen to see an example

function totalsObject() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount, itemkey ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 };
 this.addDebit = function (amount) {
 add( amount, 'totalDebit')
 }
 ....
}

What is wrong with this javascript code?

The full code can be seen here - http://codepen.io/liam-ryan/pen/GgOpvK?editors=001

My intention is to create a nice DRY method ( add ) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here, can anyone help me out?

PS the intention is to be able to specify different objects with add, so if I had a label object called food with a credit property I could create a function called addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food').

See codepen to see an example

function totalsObject() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount, itemkey ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 };
 this.addDebit = function (amount) {
 add( amount, 'totalDebit')
 }
 ....
}

Method for parsing floats and adding totals

The full code can be seen here - http://codepen.io/liam-ryan/pen/GgOpvK?editors=001

My intention is to create a nice DRY method ( add ) which will take care of parsing floats and adding totals internally and will be called by other methods.

I feel like I'm jumping through unnecessary complex hoops here, can anyone help me out?

PS: the intention is to be able to specify different objects with add, so if I had a label object called food with a credit property I could create a function called addFoodCredit(amount) which would call add( amount, 'credit', 'labels', 'food').

See codepen to see an example

function totalsObject() {
 var totals = {
 totalDebit: 0,
 totalCredit: 0,
 labels: {}
 }
 var add = function add( amount, itemkey ) {
 var args = Array.prototype.slice.call(arguments) 
 var objectReference = totals;
 var floatAmount = parseFloat (args.shift());
 var objectKey = args.shift();
 while(args.length > 0) {
 objectReference = objectReference[args.shift()];
 }
 var currentTotal = parseFloat( objectReference[objectKey] );
 objectReference[objectKey] = currentTotal + floatAmount;
 };
 this.addDebit = function (amount) {
 add( amount, 'totalDebit')
 }
 ....
}
Post Closed as "Not suitable for this site" by Vogel612, Jamal
Source Link
LiamRyan
  • 313
  • 2
  • 9
Loading
default

AltStyle によって変換されたページ (->オリジナル) /