0

Why do the isSingleand household arguments not seem to pass to the appropriate properties of the Person object when creating them and pushing them to kidsArr at the end of the script inside the switch while the instances created earlier in the script and being pushed to s20to24Arrseem to be getting the arguments properly? What am I missing?

startPop = 1000

var getRandom = function(min, max) {
  return Math.random() * (max - min) + min;
};

var lastNames = ['SMITH', 'JOHNSON', 'WILLIAMS', 'JONES', 'BROWN', 'DAVIS', 'MILLER'];
var firstNamesMale = ["Michael", "Christopher", "Jason", "David", "James", "John"];
var firstNamesFemale = ["Jennifer", "Amy", "Melissa", "Michelle", "Kimberly"];

var sex = ["Male", "Female"];

var genSex = function() {
    var index = Math.round(getRandom(0, sex.length - 1));
    return sex[index];
};

var genLastName = function() {
  var index = Math.round(getRandom(0, lastNames.length - 1));
  return lastNames[index];
};

var genFirstNameMale = function() {
  var index = Math.round(getRandom(0, firstNamesMale.length - 1));
  return firstNamesMale[index];
};

var genFirstNameFemale = function() {
  var index = Math.round(getRandom(0, firstNamesFemale.length - 1));
  return firstNamesFemale[index];
};

var s20to24Arr = [];
var householdArray = [];
var kidsArr = [];

var sing20to24 = Math.round(startPop * 0.088 * 0.8385);

var Person = function(sex, age, isSingle, lastName, household) {
  if (sex === undefined) {
    this.sex = genSex();
  } else {
    this.sex = sex;
  }
  if (lastName === undefined) {
    this.lastName = genLastName();
  } else {
    this.lastName = lastName;
  }
  this.firstName = this.sex === "Male" ? genFirstNameMale() : genFirstNameFemale();
  if (age === undefined) {
    this.age = genAgeAdult();
  } else {
    this.age = age;
  }
  //this.income = 0;
  //this.isEmployed = true;
  //this.isRetired = false;
  this.isSingle = isSingle;
  this.household = household;
};

var Household = function(man, woman) {
  this.man = man;
  this.woman = woman;
  //this.house = undefined;
  this.kid1 = undefined;
  this.kid2 = undefined;
  this.kid3 = undefined;
  //this.income : this.man.income + this.woman.income;
  //this.expenses : 0;
  //this.savings : 0;
  //this.debts : 0;
  //this.isHomeOwner : true;
};

var numKids = function(isSingle) {
  var num = getRandom(0, 100);
  if (!isSingle) {
    if (num <= 47.73) {
      return 0;
    } else if (num <= 68.22) {
      return 1;
    } else if (num <= 90.46) {
      return 2;
    } else {
      return 3;
    }
  } else {
    if (num <= 82.31) {
      return 0;
    } else if (num <= 93.04) {
      return 1;
    } else if (num <= 98.11) {
      return 2;
    } else {
      return 3;
    }
  }

};

//This creates instances of the Person object with lastName, firstName, sex, age, isSingle and household properties. It also creates an instance of the Household object.
for (var i = 0; i < sing20to24; i += 2) {
  s20to24Arr.push(new Person("Male", Math.round(getRandom(20, 24)), true));
  s20to24Arr.push(new Person("Female", Math.round(getRandom(20, 24)), true));
  var curMale = s20to24Arr[i],
    curFem = s20to24Arr[i + 1];
  householdArray.push(new Household(curMale));
  var index = householdArray.length > 0 ? householdArray.length - 1 : 0;
  curMale.household = householdArray[index];
  householdArray.push(new Household(undefined, curFem));
  curFem.household = householdArray[index + 1];
  var kids = numKids(true);
  switch (kids) {
    case 0:
      break;
    case 1:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind];
      break;
    case 2:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind - 1];
      curFem.household.kid2 = kidsArr[ind];
      break;
    case 3:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind - 2];
      curFem.household.kid2 = kidsArr[ind - 1];
      curFem.household.kid3 = kidsArr[ind];
      break;
  }
}
console.log(s20to24Arr[0]);
console.log(kidsArr[0]);

1 Answer 1

1

Are you sure you are placing parentheses in correct places?

You do this:

new Person(
    undefined, 
    Math.round(
        getRandom(0, 5),
        true,
        curFem.lastName, 
        curFem.household
    )
)

I indented the code on purpose so that you can see your error. You pass 2 parameters to the Person constructor, the second one being Math.round(...) which eats the rest of your parameters.

It should probably be like this:

new Person(
    undefined,
    Math.round(getRandom(0, 5)),
    true,
    curFem.lastName, 
    curFem.household
)

I didn't check anything else, there might be more errors but this is an overlooked basic mistake.

s20to24Arr gets correct object because you put parentheses correctly there:

s20to24Arr.push(
    new Person("Male", Math.round(getRandom(20, 24)), true) // here Math.round takes only 1 parameter as it should
);
Sign up to request clarification or add additional context in comments.

Comments

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.