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);









share|improve this question

























    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);









    share|improve this question























      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);









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 11:14









      Herp Derp

      11




      11






















          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);





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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);





            share|improve this answer
























              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);





              share|improve this answer






















                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);





                share|improve this answer












                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);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 11:34









                Jonas Wilms

                52.1k42445




                52.1k42445



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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














































































                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党