basic math and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • birdboy272
    New Member
    • May 2007
    • 10

    basic math and arrays

    This is this impossible question I have to do I dont even know how to start it.

    2) The physics equation for Force, F = m*a. (3pt)
    Means force (N) is equals to mass (kg) times by acceleration (m/s^2)
    ...Use an array to store 10 masses and another array to store 10 accelerations.
    ...Use a loop to calculate and output (document.write ) the forces generated.
    Sample output: 5(kg) * 10 (m/s^2) = 50 (N)


    ??????????????? ??????????????? ????????????
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    thats not too hard ! come on i'll help you get started but because its a class I won't give you the full code. (tsdn policies);


    so you make an array i would go about it like this

    Code:
    var masses = [1, 23, 43, 34, 51, 26, 57, 78, 89, 10];
    var mass = [masses];
    This is how an array is made

    then to pull the data just use
    Code:
    mass[0][2] this would = 23
    todo some javascript math i use Math.floor()
    Math.floor(Math functions here)


    then you need a loop a loop would be made like this for example
    Code:
    for (i=0; i<10; i++) {
    dostuff
    }

    its i<10 because it starts at 0 so it would be 10 overall when it stops at 9.


    document.write is used like this
    Code:
     
    document.write("hello people!");
    to add vars to a document.write you do
    Code:
    document.write("hello people!"+mass[0][i]+" mass!");

    Comment

    • birdboy272
      New Member
      • May 2007
      • 10

      #3
      Physics and javascript

      I need to do a physics equation using Javascript it needs to use the array variable and it needs to contain 10 different equations. I need a equation that looks something like this

      5(kg) * 10 (m/s^2) = 50 (N)

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        did I not already answer this question for you in this Thread?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by iam_clint
          did I not already answer this question for you in this Thread?
          This calls for a merging. Threads merged.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by birdboy272
            I need to do a physics equation using Javascript it needs to use the array variable and it needs to contain 10 different equations. I need a equation that looks something like this

            5(kg) * 10 (m/s^2) = 50 (N)
            You have been given enough information to attempt the problem yourself. If you don't know the basics of javascript, see the tutorial links in the Offsite Links thread at the top of this forum.

            Make an attempt and if you get stuck on a particular part, then post again.

            Comment

            • birdboy272
              New Member
              • May 2007
              • 10

              #7
              Thank you guys This is this code that i used and it worked

              <script>

              var nAcc = new Array();
              nAcc[0] = 3;
              nAcc[1] = 4;
              nAcc[2] = 5;
              nAcc[3] = 6;
              nAcc[4] = 7;
              nAcc[5] = 8;
              nAcc[6] = 9;
              nAcc[7] = 10;
              nAcc[8] = 11;
              nAcc[9] = 12;

              var nMass = new Array();
              nMass[0] = 3;
              nMass[1] = 4;
              nMass[2] = 5;
              nMass[3] = 6;
              nMass[4] = 7;
              nMass[5] = 8;
              nMass[6] = 9;
              nMass[7] = 10;
              nMass[8] = 11;
              nMass[9] = 12;
              var nForce=0;


              for (n=0; n<10; n++){
              nForce = nAcc[n] * nMass[n];
              document.write( nForce + " (N)=" + nMass[n] +"(m/s^2) * " + nAcc[n]+ "kg<br>");

              }

              </script>

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                cool good for you.... i had written this code out before i told you how to do it... i made mine with a nested loop for all possibilities.
                [CODE=javascript]
                <script>
                function calcForce() {
                var masses = [2, 4, 5, 12, 1232, 289, 925, 892, 102, 383];
                var mass = [masses];
                var accelerations = [20, 12, 44, 218, 12, 33, 11, 22, 23, 78];
                var acceleration = [accelerations];
                for (i=0; i<10; i++) {
                for (b=0; b<10; b++) {
                document.write( mass[0][i]+"(kg) * "+accelerat ion[0][b]+"(m/s^2) = "+Math.floor(ma ss[0][i]*acceleration[0][b])+" (N)<br>");
                }
                }
                }
                </script>
                <input type="button" onclick="calcFo rce()" value="Test">
                [/CODE]

                Comment

                Working...