PDA

View Full Version : Javascript Fun!



Clouded Sky
05-30-2009, 08:04 AM
So I have to write a javascript program, but unfortunately I don't understand anything the teacher ever says. I'm serious. I'm not the only one either. I have several friends in that class and we're all confused. I was hoping you could help!

I think I have a basic understanding but not really exactly how to go about doing it. Basically I have to do a "model society" with 100 people. Each person has to have a random age between 20 and 50. They also need a 50/50 chance to have children (between 1 and 5) which get counted as full people. Then you age everyone until the hit a hundred and die, and since no new children are being born, everyone eventually dies.

I've been trying to read as much as I can online, but I'm getting stuck.

Make a person class with people objects? Give each object an age and an ID, right? Then basically, run a function that states while age < 100 do age++ and another that when age = 100 they die? Which consists of getting rid of the object? I guess my biggest problem is not knowing how to make ahundred people, and then making their children count as people too. Eff I'm screwed. Does this even make sense?

o_O
06-03-2009, 04:06 PM
I wouldn't say using classes is the conventional way to accomplish stuff in Javascript, but it can work, nonetheless. What you need to do is break the problem down into smaller parts. Creating the person class should come first (pretty straightforward):

function Person() {}Then you should add your prototypes. You should have a prototype for each class variable and for each variable you should have an accessor function to return that value:
function Person() {}

Person.prototype._age;
Person.prototype._numKids;

Person.prototype.age = function() { return this._age; }
Person.prototype.numKids = function() { return this._numKids; }Then you should create some sort of variable initialisation in the constructor of your class. Ideally in your case, this will create an age and a number of kids (if any):
function Person() {
this._age = Math.random()*30+20;
this._numKids = <span style="color:red;">(Math.random() < 0.5)</span>?<span style="color:blue;">Math.random()*5+1</span>:<span style="color:green;">0</span>;
// For reference, this is a shorthand IF expression is of the form: <span style="color:red;">(condition)</span>?<span style="color:blue;">value to return if condition is true</span>:<span style="color:green;">value to return if condition is false</span>;
// In other words, numKids = random number from 1-5 if condition < 0.5, else numKids = 0. Math.random() always returns a number between 0 and 1, so there's a 50/50 chance of it evaluating to less than 0.5, hence it being your condition.
}

Person.prototype._age;
Person.prototype._numKids;

Person.prototype.age = function() { return this._age; }
Person.prototype.numKids = function() { return this._numKids; }This should get you started. Other things to consider:
<ul><li>Creating the children as People objects in the constructor of Person.</li>
<li>Creating a function for the Person class that increases the age of the Person by 1.</li>
<li>I'm not really sure what the point of aging them to 100 is, so whilst reading my code bear in mind that there might be something there that you need to think about.</li>
<li>Initialising the collection of 100 Person objects. You should use an array for this, eg.
var personArray = new Array();
for (var i = 0; i < 100; i++) personArray[i] = new Person();</li>
</ul>