Javascript Undefined Array values not connecting to function? How to run a randomized trait function to make each array contain unique traits?
up vote
0
down vote
favorite
First off, my apologies if the title is vague at all, I'm pretty new to javascript.
When this code is run it should create 2 alert windows. One for each of Bob's K characteristics. Of course all of bob 2's characteristics are undefined right now because I can't figure out how to get my SpawnInnerCharacteristics function to apply to all my people.
I need to figure out how to assign bob2 (and future bob3, and bob 4, etc) their own innercharacteristics from the innercharacteristics function. To do this, I need help with A) Changing the "Bob." inside the spawnInnerCharacteristics function to something that can accomodate more than just that singular bob. and B) re-run the SpawnInnerCharacteristics function for every new Bob created so they can all have their own unique characteristics. And whatever else I am missing to make those two things happen correctly.
Also, an optional 3rd question while we're at it, how would I go about generating a random amount of bobs (Each containing their own inner characteristics using my SpawnInnerCharacteristics funtion, not carbon copies)?
function rand(maxvalue)
return Math.floor(Math.random() * Math.floor(maxvalue));
function person(id, c, nrg, e, K_, E_, D_, A_, C_, G_, B_, M_, SM_, V_, S_, T_, X_, H_, Z_,int_)
this.id = id;
this.color = c;
this.nrg = nrg;
this.ec = e;
this.K = K_;
this.E = E_;
this.D = D_;
this.A = A_;
this.C = C_;
this.G = G_;
this.B = B_;
this.M = M_;
this.SM = SM_;
this.V = V_;
this.S = S_;
this.T = T_;
this.X = X_;
this.H = H_;
this.Z = Z_;
this.int = int_;
var Mom = new person(1,"c","nrg","brwn", "K kbr", "E e", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 2);
var Dad = new person(2,"c","nrg","black", "k kbr", "E E", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 5);
var Bob = new person(spawnInnerCharacteristics);
var Bob2 = new person(spawnInnerCharacteristics);
function spawnInnerCharacteristics()
Bob.K = physicalCharacteristics(Mom.K, Dad.K)
Bob.E = physicalCharacteristics(Mom.E, Dad.E)
Bob.D = physicalCharacteristics(Mom.D, Dad.D)
Bob.A = physicalCharacteristics(Mom.A, Dad.A)
Bob.C = physicalCharacteristics(Mom.C, Dad.C)
Bob.G = physicalCharacteristics(Mom.G, Dad.G)
Bob.B = physicalCharacteristics(Mom.B, Dad.B)
Bob.M = physicalCharacteristics(Mom.M, Dad.M)
Bob.SM = physicalCharacteristics(Mom.SM, Dad.SM)
Bob.V = physicalCharacteristics(Mom.V, Dad.V)
Bob.S = physicalCharacteristics(Mom.S, Dad.S)
Bob.T = physicalCharacteristics(Mom.T, Dad.T)
Bob.X = physicalCharacteristics(Mom.X, Dad.X)
Bob.H = physicalCharacteristics(Mom.H, Dad.H)
Bob.Z = physicalCharacteristics(Mom.Z, Dad.Z)
Bob.int = mentalCharacteristics(Mom.int, Dad.int)
return spawnInnerCharacteristics;
function physicalCharacteristics(a, b)
var PA = [a.split(" ") , b.split(" ")];
var AM = PA[0];
var AD = PA[1];
var CA = AD[Math.floor(Math.random()*AD.length)];
var LA = AM[Math.floor(Math.random()*AM.length)];
return LA + " " + CA;
function mentalCharacteristics(a, b)
return ((a*1 + b*1) /2 + rand(3));
spawnInnerCharacteristics();
window.alert("This is bob 1's K characteristic. Every once in a while, if the code is working how I'd like it to, the K's will vary between the two bobs: " + Bob.K);
window.alert("This is bob 2's K characteristic.: " + Bob2.K);
javascript arrays function this
add a comment |
up vote
0
down vote
favorite
First off, my apologies if the title is vague at all, I'm pretty new to javascript.
When this code is run it should create 2 alert windows. One for each of Bob's K characteristics. Of course all of bob 2's characteristics are undefined right now because I can't figure out how to get my SpawnInnerCharacteristics function to apply to all my people.
I need to figure out how to assign bob2 (and future bob3, and bob 4, etc) their own innercharacteristics from the innercharacteristics function. To do this, I need help with A) Changing the "Bob." inside the spawnInnerCharacteristics function to something that can accomodate more than just that singular bob. and B) re-run the SpawnInnerCharacteristics function for every new Bob created so they can all have their own unique characteristics. And whatever else I am missing to make those two things happen correctly.
Also, an optional 3rd question while we're at it, how would I go about generating a random amount of bobs (Each containing their own inner characteristics using my SpawnInnerCharacteristics funtion, not carbon copies)?
function rand(maxvalue)
return Math.floor(Math.random() * Math.floor(maxvalue));
function person(id, c, nrg, e, K_, E_, D_, A_, C_, G_, B_, M_, SM_, V_, S_, T_, X_, H_, Z_,int_)
this.id = id;
this.color = c;
this.nrg = nrg;
this.ec = e;
this.K = K_;
this.E = E_;
this.D = D_;
this.A = A_;
this.C = C_;
this.G = G_;
this.B = B_;
this.M = M_;
this.SM = SM_;
this.V = V_;
this.S = S_;
this.T = T_;
this.X = X_;
this.H = H_;
this.Z = Z_;
this.int = int_;
var Mom = new person(1,"c","nrg","brwn", "K kbr", "E e", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 2);
var Dad = new person(2,"c","nrg","black", "k kbr", "E E", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 5);
var Bob = new person(spawnInnerCharacteristics);
var Bob2 = new person(spawnInnerCharacteristics);
function spawnInnerCharacteristics()
Bob.K = physicalCharacteristics(Mom.K, Dad.K)
Bob.E = physicalCharacteristics(Mom.E, Dad.E)
Bob.D = physicalCharacteristics(Mom.D, Dad.D)
Bob.A = physicalCharacteristics(Mom.A, Dad.A)
Bob.C = physicalCharacteristics(Mom.C, Dad.C)
Bob.G = physicalCharacteristics(Mom.G, Dad.G)
Bob.B = physicalCharacteristics(Mom.B, Dad.B)
Bob.M = physicalCharacteristics(Mom.M, Dad.M)
Bob.SM = physicalCharacteristics(Mom.SM, Dad.SM)
Bob.V = physicalCharacteristics(Mom.V, Dad.V)
Bob.S = physicalCharacteristics(Mom.S, Dad.S)
Bob.T = physicalCharacteristics(Mom.T, Dad.T)
Bob.X = physicalCharacteristics(Mom.X, Dad.X)
Bob.H = physicalCharacteristics(Mom.H, Dad.H)
Bob.Z = physicalCharacteristics(Mom.Z, Dad.Z)
Bob.int = mentalCharacteristics(Mom.int, Dad.int)
return spawnInnerCharacteristics;
function physicalCharacteristics(a, b)
var PA = [a.split(" ") , b.split(" ")];
var AM = PA[0];
var AD = PA[1];
var CA = AD[Math.floor(Math.random()*AD.length)];
var LA = AM[Math.floor(Math.random()*AM.length)];
return LA + " " + CA;
function mentalCharacteristics(a, b)
return ((a*1 + b*1) /2 + rand(3));
spawnInnerCharacteristics();
window.alert("This is bob 1's K characteristic. Every once in a while, if the code is working how I'd like it to, the K's will vary between the two bobs: " + Bob.K);
window.alert("This is bob 2's K characteristic.: " + Bob2.K);
javascript arrays function this
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
First off, my apologies if the title is vague at all, I'm pretty new to javascript.
When this code is run it should create 2 alert windows. One for each of Bob's K characteristics. Of course all of bob 2's characteristics are undefined right now because I can't figure out how to get my SpawnInnerCharacteristics function to apply to all my people.
I need to figure out how to assign bob2 (and future bob3, and bob 4, etc) their own innercharacteristics from the innercharacteristics function. To do this, I need help with A) Changing the "Bob." inside the spawnInnerCharacteristics function to something that can accomodate more than just that singular bob. and B) re-run the SpawnInnerCharacteristics function for every new Bob created so they can all have their own unique characteristics. And whatever else I am missing to make those two things happen correctly.
Also, an optional 3rd question while we're at it, how would I go about generating a random amount of bobs (Each containing their own inner characteristics using my SpawnInnerCharacteristics funtion, not carbon copies)?
function rand(maxvalue)
return Math.floor(Math.random() * Math.floor(maxvalue));
function person(id, c, nrg, e, K_, E_, D_, A_, C_, G_, B_, M_, SM_, V_, S_, T_, X_, H_, Z_,int_)
this.id = id;
this.color = c;
this.nrg = nrg;
this.ec = e;
this.K = K_;
this.E = E_;
this.D = D_;
this.A = A_;
this.C = C_;
this.G = G_;
this.B = B_;
this.M = M_;
this.SM = SM_;
this.V = V_;
this.S = S_;
this.T = T_;
this.X = X_;
this.H = H_;
this.Z = Z_;
this.int = int_;
var Mom = new person(1,"c","nrg","brwn", "K kbr", "E e", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 2);
var Dad = new person(2,"c","nrg","black", "k kbr", "E E", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 5);
var Bob = new person(spawnInnerCharacteristics);
var Bob2 = new person(spawnInnerCharacteristics);
function spawnInnerCharacteristics()
Bob.K = physicalCharacteristics(Mom.K, Dad.K)
Bob.E = physicalCharacteristics(Mom.E, Dad.E)
Bob.D = physicalCharacteristics(Mom.D, Dad.D)
Bob.A = physicalCharacteristics(Mom.A, Dad.A)
Bob.C = physicalCharacteristics(Mom.C, Dad.C)
Bob.G = physicalCharacteristics(Mom.G, Dad.G)
Bob.B = physicalCharacteristics(Mom.B, Dad.B)
Bob.M = physicalCharacteristics(Mom.M, Dad.M)
Bob.SM = physicalCharacteristics(Mom.SM, Dad.SM)
Bob.V = physicalCharacteristics(Mom.V, Dad.V)
Bob.S = physicalCharacteristics(Mom.S, Dad.S)
Bob.T = physicalCharacteristics(Mom.T, Dad.T)
Bob.X = physicalCharacteristics(Mom.X, Dad.X)
Bob.H = physicalCharacteristics(Mom.H, Dad.H)
Bob.Z = physicalCharacteristics(Mom.Z, Dad.Z)
Bob.int = mentalCharacteristics(Mom.int, Dad.int)
return spawnInnerCharacteristics;
function physicalCharacteristics(a, b)
var PA = [a.split(" ") , b.split(" ")];
var AM = PA[0];
var AD = PA[1];
var CA = AD[Math.floor(Math.random()*AD.length)];
var LA = AM[Math.floor(Math.random()*AM.length)];
return LA + " " + CA;
function mentalCharacteristics(a, b)
return ((a*1 + b*1) /2 + rand(3));
spawnInnerCharacteristics();
window.alert("This is bob 1's K characteristic. Every once in a while, if the code is working how I'd like it to, the K's will vary between the two bobs: " + Bob.K);
window.alert("This is bob 2's K characteristic.: " + Bob2.K);
javascript arrays function this
First off, my apologies if the title is vague at all, I'm pretty new to javascript.
When this code is run it should create 2 alert windows. One for each of Bob's K characteristics. Of course all of bob 2's characteristics are undefined right now because I can't figure out how to get my SpawnInnerCharacteristics function to apply to all my people.
I need to figure out how to assign bob2 (and future bob3, and bob 4, etc) their own innercharacteristics from the innercharacteristics function. To do this, I need help with A) Changing the "Bob." inside the spawnInnerCharacteristics function to something that can accomodate more than just that singular bob. and B) re-run the SpawnInnerCharacteristics function for every new Bob created so they can all have their own unique characteristics. And whatever else I am missing to make those two things happen correctly.
Also, an optional 3rd question while we're at it, how would I go about generating a random amount of bobs (Each containing their own inner characteristics using my SpawnInnerCharacteristics funtion, not carbon copies)?
function rand(maxvalue)
return Math.floor(Math.random() * Math.floor(maxvalue));
function person(id, c, nrg, e, K_, E_, D_, A_, C_, G_, B_, M_, SM_, V_, S_, T_, X_, H_, Z_,int_)
this.id = id;
this.color = c;
this.nrg = nrg;
this.ec = e;
this.K = K_;
this.E = E_;
this.D = D_;
this.A = A_;
this.C = C_;
this.G = G_;
this.B = B_;
this.M = M_;
this.SM = SM_;
this.V = V_;
this.S = S_;
this.T = T_;
this.X = X_;
this.H = H_;
this.Z = Z_;
this.int = int_;
var Mom = new person(1,"c","nrg","brwn", "K kbr", "E e", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 2);
var Dad = new person(2,"c","nrg","black", "k kbr", "E E", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 5);
var Bob = new person(spawnInnerCharacteristics);
var Bob2 = new person(spawnInnerCharacteristics);
function spawnInnerCharacteristics()
Bob.K = physicalCharacteristics(Mom.K, Dad.K)
Bob.E = physicalCharacteristics(Mom.E, Dad.E)
Bob.D = physicalCharacteristics(Mom.D, Dad.D)
Bob.A = physicalCharacteristics(Mom.A, Dad.A)
Bob.C = physicalCharacteristics(Mom.C, Dad.C)
Bob.G = physicalCharacteristics(Mom.G, Dad.G)
Bob.B = physicalCharacteristics(Mom.B, Dad.B)
Bob.M = physicalCharacteristics(Mom.M, Dad.M)
Bob.SM = physicalCharacteristics(Mom.SM, Dad.SM)
Bob.V = physicalCharacteristics(Mom.V, Dad.V)
Bob.S = physicalCharacteristics(Mom.S, Dad.S)
Bob.T = physicalCharacteristics(Mom.T, Dad.T)
Bob.X = physicalCharacteristics(Mom.X, Dad.X)
Bob.H = physicalCharacteristics(Mom.H, Dad.H)
Bob.Z = physicalCharacteristics(Mom.Z, Dad.Z)
Bob.int = mentalCharacteristics(Mom.int, Dad.int)
return spawnInnerCharacteristics;
function physicalCharacteristics(a, b)
var PA = [a.split(" ") , b.split(" ")];
var AM = PA[0];
var AD = PA[1];
var CA = AD[Math.floor(Math.random()*AD.length)];
var LA = AM[Math.floor(Math.random()*AM.length)];
return LA + " " + CA;
function mentalCharacteristics(a, b)
return ((a*1 + b*1) /2 + rand(3));
spawnInnerCharacteristics();
window.alert("This is bob 1's K characteristic. Every once in a while, if the code is working how I'd like it to, the K's will vary between the two bobs: " + Bob.K);
window.alert("This is bob 2's K characteristic.: " + Bob2.K);
javascript arrays function this
javascript arrays function this
asked Nov 10 at 11:14
Herp Derp
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
A few things went wrong in your code:
1) passing a function as id
makes little sense here:
var Bob = new person(spawnInnerCharacteristics);
2) spawnInnerCharacteristics
always sets the properties of Bob
based on Mom
and Dad
.
3) While not following naming conventions is "not an error", it makes code easier to read by others if you capitalize constructors (Person
) and name all the other things in camelCase (bob
, dad
).
To make that dynamic, you could pass in the two parents and the child as arguments to the function:
function spawnInnerCharacteristics(child, mom, dad)
child.k = physicalCharacteristics(mom.k, dad.k);
//...
That way you can easily create multiple persons and pass them:
const mom = new Person(/*...*/);
const dad = new Person(/*...*/);
const alice = new Person(/*...*/);
const bob = new Person(/*...*/);
spawnInnerCharacteristics(alice, mom, dad);
spawnInnerCharacterisitcs(bob, mom, dad);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
A few things went wrong in your code:
1) passing a function as id
makes little sense here:
var Bob = new person(spawnInnerCharacteristics);
2) spawnInnerCharacteristics
always sets the properties of Bob
based on Mom
and Dad
.
3) While not following naming conventions is "not an error", it makes code easier to read by others if you capitalize constructors (Person
) and name all the other things in camelCase (bob
, dad
).
To make that dynamic, you could pass in the two parents and the child as arguments to the function:
function spawnInnerCharacteristics(child, mom, dad)
child.k = physicalCharacteristics(mom.k, dad.k);
//...
That way you can easily create multiple persons and pass them:
const mom = new Person(/*...*/);
const dad = new Person(/*...*/);
const alice = new Person(/*...*/);
const bob = new Person(/*...*/);
spawnInnerCharacteristics(alice, mom, dad);
spawnInnerCharacterisitcs(bob, mom, dad);
add a comment |
up vote
0
down vote
A few things went wrong in your code:
1) passing a function as id
makes little sense here:
var Bob = new person(spawnInnerCharacteristics);
2) spawnInnerCharacteristics
always sets the properties of Bob
based on Mom
and Dad
.
3) While not following naming conventions is "not an error", it makes code easier to read by others if you capitalize constructors (Person
) and name all the other things in camelCase (bob
, dad
).
To make that dynamic, you could pass in the two parents and the child as arguments to the function:
function spawnInnerCharacteristics(child, mom, dad)
child.k = physicalCharacteristics(mom.k, dad.k);
//...
That way you can easily create multiple persons and pass them:
const mom = new Person(/*...*/);
const dad = new Person(/*...*/);
const alice = new Person(/*...*/);
const bob = new Person(/*...*/);
spawnInnerCharacteristics(alice, mom, dad);
spawnInnerCharacterisitcs(bob, mom, dad);
add a comment |
up vote
0
down vote
up vote
0
down vote
A few things went wrong in your code:
1) passing a function as id
makes little sense here:
var Bob = new person(spawnInnerCharacteristics);
2) spawnInnerCharacteristics
always sets the properties of Bob
based on Mom
and Dad
.
3) While not following naming conventions is "not an error", it makes code easier to read by others if you capitalize constructors (Person
) and name all the other things in camelCase (bob
, dad
).
To make that dynamic, you could pass in the two parents and the child as arguments to the function:
function spawnInnerCharacteristics(child, mom, dad)
child.k = physicalCharacteristics(mom.k, dad.k);
//...
That way you can easily create multiple persons and pass them:
const mom = new Person(/*...*/);
const dad = new Person(/*...*/);
const alice = new Person(/*...*/);
const bob = new Person(/*...*/);
spawnInnerCharacteristics(alice, mom, dad);
spawnInnerCharacterisitcs(bob, mom, dad);
A few things went wrong in your code:
1) passing a function as id
makes little sense here:
var Bob = new person(spawnInnerCharacteristics);
2) spawnInnerCharacteristics
always sets the properties of Bob
based on Mom
and Dad
.
3) While not following naming conventions is "not an error", it makes code easier to read by others if you capitalize constructors (Person
) and name all the other things in camelCase (bob
, dad
).
To make that dynamic, you could pass in the two parents and the child as arguments to the function:
function spawnInnerCharacteristics(child, mom, dad)
child.k = physicalCharacteristics(mom.k, dad.k);
//...
That way you can easily create multiple persons and pass them:
const mom = new Person(/*...*/);
const dad = new Person(/*...*/);
const alice = new Person(/*...*/);
const bob = new Person(/*...*/);
spawnInnerCharacteristics(alice, mom, dad);
spawnInnerCharacterisitcs(bob, mom, dad);
answered Nov 10 at 11:34
Jonas Wilms
52.1k42445
52.1k42445
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238379%2fjavascript-undefined-array-values-not-connecting-to-function-how-to-run-a-rando%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password