0

I am trying to create a function that will dynamically create objects on the fly based on the input number arguments, but I'm running into an issue with iterating over the Object.create() part. I don't know where to play my i in the for loop, but ideally I would have all the sportsCar objects stored in the sportArray. That is the target at least.

function car(doors, capacity, storage) {
  this.doors = doors;
  this.capacity = capacity;
  this.storage = storage;
};


var van = Object.create(car);
van.doors = 4;
van.storage = "rear storage";

var miniVan = Object.create(van);
miniVan.capacity = "200 LB";

var cargoVan = Object.create(van);
cargoVan.capacity = "800 LB";

var truck = Object.create(car);
truck.doors = 2;
truck.storage = "bed";
truck.capacity = "1500 LB";

var familyCar = Object.create(car);
familyCar.doors = 4;
familyCar.storage = "large trunk";
familyCar.capacity = "300 LB";

var sportsCar = Object.create(car);
sportsCar.doors = 2;
sportsCar.storage = "small trunk";
sportsCar.capacity = '100 LB'; 

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

    sportsObjs = for(var i = 0; i < n1; i++){
       Object.create(sportsCar);
    }

    sportArray.push(sportsObjs);

    for (var i = 0; i < n2; i++){
       Object.create(familyCar);
    }

    for(var i = 0; i < n3; i++){
       Object.create(truck)
    }

    for(var i = 0; i < n4; i++){
        Object.create(miniVan)
    }

    for(var i = 0; i < n5; i++){
        Object.create(cargoVan)
    }

    return console.log(sportsArray);

}
7
  • Object.create(car) does not work as you expect when car is a constructor function. You want car to be a prototype object - make it an object literal. Commented May 31, 2016 at 15:46
  • @Bergi can you clarify for me? Im used to class based inheritance and I thought I set this up correctly by ecma5 standards Commented May 31, 2016 at 15:53
  • @Bergi I would like to be able to access all the .capacity properties in my tally function ideally :/ Commented May 31, 2016 at 15:56
  • Object.create does not create a subclass. It can create a subclass .prototype object, but for that your Van, MiniVan, Truck etc would need to be constructor functions and you'd need to assign their .prototypes. Since your constructors aren't doing much initialisation, I thought you'd rather go by plain prototypical inheritance. Commented May 31, 2016 at 16:31
  • I see. I fixed it up a bunch @Bergi, but can you illuminate why my function isnt populating the array now like before? I tested to make sure all my objects in the chain were outputing their properties correctly jsfiddle.net/h7uuxjaw Commented May 31, 2016 at 16:35

2 Answers 2

1
sportsObjs = for(var i = 0; i < n1; i++){
   Object.create(sportsCar);
}

sportArray.push(sportsObjs);

That's a plain syntax error. A loop is a statement in JavaScript, not an expression - it doesn't yield a value. You can't assign it to a variable. What you actually want is to assign each newly created object to the variable, and then push that particular new object to the array:

for (var i = 0; i < n1; i++){
    var sportsObj = Object.create(sportsCar);
    sportArray.push(sportsObj);
}
Sign up to request clarification or add additional context in comments.

2 Comments

oh duh. now i feel dumb. early morning I guess. Thank you for your help
i will definitely do so, but this is a new account and I need to get 15 pts to upvote lol. behind the times. I will accept in 4 minutes :)
0

You need only to push objects in the array inside the loop:

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

    for(var i = 0; i < n1; i++){
        sportArray.push(Object.create(sportsCar));   // To create a generic Object   sportArray.push({});
    }

    ....  // And so on for all the arrays
}

The problem is that you declared the different arrays as var so they are not visible outside the body of the function.

You need to return an object containg all the arrays, something like that:

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

    ...

    return {
        sportArray: sportArray,
        familyArray : familyArray,
        truckArray: truckArray,
        miniArray: miniArray,
        cargoArray: cargoArray
    }
}

So you can do something like:

var result = tally(3, 4, 5, 6, 7);
console.log(result.sportArray.length);

To be more succint with parameters:

function tally(parameters) { ... for (var i = 0; i < parameters.n1; i++) { ... } ... }

Calling tally in this manner:

var result = tally({n1: 3, n2:4, n3:5, n4:6, n5:7}); 

10 Comments

I dont think that will work due to how i created my object chain. See updated post with more code
@user5775744 it works, but it is not very useful unless you change what you return as I explained in the answer
is there anyway I can be more succinct with my parameters, like pass in the argument param?
Yes you can create an object that holds all the values you need, something like {n1: 3, n2:4, n3:5, n5:6, n:7}, but you need to update the code in the same manner
but that defeats the purpose cause I want those n values to be variable in the sense that n1 could maybe be 3 or it could also be 12. Isnt there an argument param in ecma5?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.