0

I know the pattern that basically tells you to create a js object and then pass in that object as a param into the parent function, but I have a slight twist on this that I wanted to see if was possible.

Say I have the following parent function

 //helper functions
 function handleArray(array, target, n) {
   for (var i = 0; i < n; i++) {
     array.push(new target());
      } 
    }

//parent function

function tally(n1, n2, n3, n4, n5, cargo) {
  var sportArray = [];
  var familyArray = [];
  var truckArray = [];
  var miniArray = [];
  var cargoArray = [];
  var output =[];
  var parsed = [];
  var names = [];

//buids out my objects based on helper function above
handleArray(sportArray,SportsCar, n1);
handleArray(familyArray,FamilyCar, n2);
handleArray(truckArray,Truck, n3);
handleArray(miniArray,MiniVan, n4);
handleArray(cargoArray,CargoVan, n5);


//storing all the cars together in mega array
var carArray = sportArray.concat(familyArray, truckArray, miniArray, cargoArray);

//gives us a mapped array of just the capacity values stored in one neat array
var carsMapped = carArray.map( function(o) { return o.capacity; });

var startCargo = cargo; //maintain original in memory

var sumTotal = carsMapped.reduce(function(prev,curr){
    return prev + curr;
});


// strggling hard core here maybe revise OO properties to make easier?
// var test = full(carsMapped, cargo);

// console.log();




//now for the printing finale!!!!
console.log("Allocating " + startCargo + " LB of cargo in total");

for(var i = 0; i < carArray.length; i++) {
   names.push(carArray[i].type);
   parsed.push(carArray[i].capacity);
   console.log( "A " + names[i] + " with " + parsed[i] + " LBs");
}
console.log("We have " + cargo + " LBs of cargo left over.")
}


// //test output
tally(1,3,4,2,1, 7356);
// tally(1, 6, 1, 2, 3, 7356);

// // tally(1,1,1,1,1);
// // tally(0,0,1,0,0);

I want to be able to condense n1..n5 arguments into ideally just two arguments. However if I understand the options object param it would be something like this:

var options = {
sport: 'n1',
family: 'n2',
truck: n3,
mini: n4,
cargo: n5
}

function tally(options, cargo){...;}

This is fine except I cant actuallly set the values of n1..n5 like before hand and this is critical to my parent function tally to work because n1..n5 represent different objects that are created on the fly.

Any patterns or tips on how I could use less arguments while working around these issues?

Thanks~

9
  • 1
    For what it's worth, you have the object pattern backwards. You would create an options object where each key matches an argument (n1, n2, etc.) and the value would be the value you want to pass for that argument. n1: 'sport', n2: 'family' etc. Commented Jun 1, 2016 at 16:09
  • ah I see, but do you generally understand my larger issue? I just dont like having that many arguments in my parent function. I know in ruby they have *args. Anything similar? Commented Jun 1, 2016 at 16:10
  • @HarryLim so are you saying by your pattern I'd have something like this {n1 : value , n2 : value2, ... n5: value5} and then set the values of them like options.n1 = 5 for example? Commented Jun 1, 2016 at 16:13
  • You can do so, also i remove the comment because the first comment already mentioned it. Commented Jun 1, 2016 at 16:14
  • 1
    I have no idea what you mean by "I cant actuallly set the values of n1..n5 like before hand". Can you show more of your code - the function declaration, how the parameter(s) are used, and how the function is called? Commented Jun 1, 2016 at 16:19

2 Answers 2

0

Firstly, you had the order of the object's values backwards. It should be key: value, not the other way around. You use an object as the parameter when you call the method, but it doesn't have to be create beforehand, it can be done in the parameters like this.

tally ({n1: 'sport', n2: 'family', n3: 'truck', n4: 'mini', n5: 'cargo'});

Or, if you prefer making an object beforehand:

var options = {
    n1: 'sport',
    n2: 'family',
    n3: 'truck',
    n4: 'mini',
    n5: 'cargo'
}

tally (options);

Where the function would be written as follows: You can easily access the object's values by doing object.key (where the keys are, in this case, n1-n5, and the object is data).

function tally (data) {
    var n1 = data.n1; //in this example, n1 = 'sport'
    var n2 = data.n2; //and n2 = 'family'
    //...
    //Do stuff
}

If you wanted the first parameter value to be 4, you could do:

tally ({n1: 4, ...})

and if you wanted 10, you could do:

tally ({n1: 10, ...})

or if you just wanted it to match a previous variable x, you could do:

tally ({n1: x, ...})

EDIT: From your edited question code, you would do the following (only showing changed code for readability):

function tally(data, cargo) {
  var sportArray = [];
  var familyArray = [];
  var truckArray = [];
  var miniArray = [];
  var cargoArray = [];
  var output =[];
  var parsed = [];
  var names = [];

handleArray(sportArray,SportsCar, data.n1);
handleArray(familyArray,FamilyCar, data.n2);
handleArray(truckArray,Truck, data.n3);
handleArray(miniArray,MiniVan, data.n4);
handleArray(cargoArray,CargoVan, data.n5);

//Unchanged code removed

//test output
tally({n1: 1, n2: 3, n3: 4, n4: 2, n5: 1}, 7356);
tally({n1: 1, n2: 6, n3: 1, n4: 2, n5: 3}, 7356);
tally({n1: 1, n2: 1, n3: 1, n4: 1, n5: 1}, 1);
tally({n1: 0, n2: 0, n3: 1, n4: 0, n5: 0}, 0);
Sign up to request clarification or add additional context in comments.

12 Comments

seems like that is just as argument filled as the previous way. :/
@user5775744 I updated the answer.
You forgot to declare the n1, n2 variables as local to the function
thank you this is a good comprehensive answer with code
@Bergi oh yeah, oops. Thanks, I'll do that.
|
0

How about this type of solution:

function tally() {
   var cargo = Array.prototype.pop.call(arguments);
   if (arguments.length == 2) {
      var options = arguments[0];
   } else {
      var options = {
        sport: arguments[0], 
        ...
      }
   }
}

4 Comments

What does the -1 do?
It's about getting the last item of an array, e.g. var a = [1,2,3]; a.pop(-1); returns -1, and a is now [1,2]
pop takes no parameters.
Sorry, I got confused with slice. So the -1 is useless. Will edit that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.