How To Build A Formula Using Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fanchun
    New Member
    • Aug 2007
    • 7

    How To Build A Formula Using Javascript?

    I already built 2 javacript files factor.js and parm.js. factor.js is an array, having several factor as elements. for example, factor.js looks like:[code=javascript]
    var factor=["intercept" , "x1","x2","x3", x4","x5"][/code]
    parm.js is also an array, having coresponding parameters to those factors in factor.js file. it looks like[code=javascript]
    var parm=["0.5", "0.1", "-0.2","0","1","0 .3"][/code]

    I want to build an regression formula based on the factors and parameters in these two js file. for example, [code=javascript]y= 0.5+x1*0.1+x2*(-0.2)+x3*0+x4*1+ x5*0.3[/code]

    i want to build text box for inputing the values of factors, like
    input x1:
    input x2:
    ...
    input x5

    so that when those factors change values, the y changes based on the formula.
    there is a button link to y, when click the button, y changes.
    how to achieve it?

    Thanks
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    welcome to TSDN ...

    it sounds like home- or coursework? please show the entire code you have so far (html and javascript) ... and tell us what the particular problem is that you have. to get the values out of the textboxes? how to update a value through the script? ... please show the efforts you already made ...

    kind regards

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, fanchun. Welcome to TSDN!

      Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

      Comment

      • fanchun
        New Member
        • Aug 2007
        • 7

        #4
        HOw to put values in text box into an array in javascript?

        i want to build a formula. and i need to build an array using values input in text boxes. i already have an array save in parmest.js.

        i want to build an formula like
        y= b0+b1*x1+b1*x2+ ....
        array of [b0,b1, b2,...] is in parmest.js;
        i need to build an array of [x1, x2,....].
        and values of x1, x2 ,.... are input in text boxes, and are changable, like
        x1:
        x2:
        ...
        i don't know how to create such array to save values input? there is some problem with tthe function in the following code:
        [code=html]
        <html>
        <head>
        <style type='text/css'>* { font-size: 10pt;}</style>
        <script type='text/javascript' src='PARMEST.js '></script> [/code][code=javascript]
        <script type='text/javascript'>
        function Go()
        {
        document.getEle mentById('resul ts1').innerHTML = "";
        var Selectedvalue = [];
        for (i=0; i < document.getEle mentByType("tex t").length; i++)
        {
        Selectedvalue.p ush(document.ge tElementByType( "text").val ue);
        }
        var S = 0;
        for (i = 0; i < PARMEST.length; i++)
        {
        S = S+PARMEST[i]*Selectedvalue[i];
        }
        document.getEle mentById('resul ts1').innerHTML =S
        } [/code][code=html]
        </script>
        </head>
        <FORM NAME="form1">
        EFFECT <br>
        <br>
        legal_dep_other 2 : <INPUT TYPE='text' NAME='legal_dep _other2 '>
        <br>
        year_in_school : <INPUT TYPE='text' NAME='year_in_s chool '>
        <br>
        total_income : <INPUT TYPE='text' NAME='total_inc ome '>
        <br>
        age : <INPUT TYPE='text' NAME='age '>
        <br>
        agi : <INPUT TYPE='text' NAME='agi '>
        <br>
        father_highest_ grade_level2 : <INPUT TYPE='text' NAME='father_hi ghest_grade_lev el2 '>
        <br>
        fm_sc : <INPUT TYPE='text' NAME='fm_sc '>
        <br>
        income : <INPUT TYPE='text' NAME='income '>
        <br>
        invest_net_wort h : <INPUT TYPE='text' NAME='invest_ne t_worth '>
        <br>
        marital_status2 1 : <INPUT TYPE='text' NAME='marital_s tatus21 '>
        <br>
        marital_status2 2 : <INPUT TYPE='text' NAME='marital_s tatus22 '>
        <br>
        mother_highest_ grade_level2 : <INPUT TYPE='text' NAME='mother_hi ghest_grade_lev el2 '>
        <br>
        num_in_college : <INPUT TYPE='text' NAME='num_in_co llege '>
        <br>
        num_in_family : <INPUT TYPE='text' NAME='num_in_fa mily '>
        <br>
        Q_KEY : <INPUT TYPE='text' NAME='Q_KEY '>
        <br>
        spouse_income : <INPUT TYPE='text' NAME='spouse_in come '>
        <br>
        veteran0 : <INPUT TYPE='text' NAME='veteran0 '>
        <br>
        <td valign='bottom' ><INPUT TYPE='button' VALUE='Calculat e' onClick='Go()'> </td>
        </FORM>
        <p><b>Actual Score <div style= 'color: #FF0000' id='results1'></div></b></p>
        </body>
        </html>[/code]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Merged duplicate threads.

          Comment

          • jx2
            New Member
            • Feb 2007
            • 228

            #6
            [code=javascript]
            x=document.getE lementsByTagNam e("input");

            //and you can use it like that:
            myValue = x[1].value;
            myName = x[1].name;

            //or
            myValue = x['myInput'].value;
            [/code]
            regards
            jx2

            Comment

            • fanchun
              New Member
              • Aug 2007
              • 7

              #7
              thanks.

              i modified 'Go' function, but still have some problem. the result does not look what i expected.

              here is the whole javascript code:
              [code=html] <html>
              <head>
              <style type='text/css'>* { font-size: 10pt;}</style>
              <script type='text/javascript' src='PARMEST.js '></script>
              <script type='text/javascript'>
              function Go()
              {
              document.getEle mentById('resul ts1').innerHTML = "";
              var x=document.getE lementsByTagNam e("input");
              var S = PARMEST[0];
              for (i = 1; i < PARMEST.length; i++)
              {
              S = S+PARMEST[i]*x[i].value;
              }
              document.getEle mentById('resul ts1').innerHTML =S
              }
              </script>
              </head>
              <FORM NAME="form1">
              EFFECT <br>
              <br>
              legal_dep_other 2 : <INPUT TYPE='text' NAME='legal_dep _other2 '>
              <br>
              year_in_school : <INPUT TYPE='text' NAME='year_in_s chool '>
              <br>
              total_income : <INPUT TYPE='text' NAME='total_inc ome '>
              <br>
              age : <INPUT TYPE='text' NAME='age '>
              <br>
              agi : <INPUT TYPE='text' NAME='agi '>
              <br>
              father_highest_ grade_level2 : <INPUT TYPE='text' NAME='father_hi ghest_grade_lev el2 '>
              <br>
              fm_sc : <INPUT TYPE='text' NAME='fm_sc '>
              <br>
              income : <INPUT TYPE='text' NAME='income '>
              <br>
              invest_net_wort h : <INPUT TYPE='text' NAME='invest_ne t_worth '>
              <br>
              marital_status2 1 : <INPUT TYPE='text' NAME='marital_s tatus21 '>
              <br>
              marital_status2 2 : <INPUT TYPE='text' NAME='marital_s tatus22 '>
              <br>
              mother_highest_ grade_level2 : <INPUT TYPE='text' NAME='mother_hi ghest_grade_lev el2 '>
              <br>
              num_in_college : <INPUT TYPE='text' NAME='num_in_co llege '>
              <br>
              num_in_family : <INPUT TYPE='text' NAME='num_in_fa mily '>
              <br>
              Q_KEY : <INPUT TYPE='text' NAME='Q_KEY '>
              <br>
              spouse_income : <INPUT TYPE='text' NAME='spouse_in come '>
              <br>
              veteran0 : <INPUT TYPE='text' NAME='veteran0 '>
              <br>
              <td valign='bottom' ><INPUT TYPE='button' VALUE='Calculat e' onClick='Go()'> </td>
              </FORM>
              <p><b>Actual Score <div style= 'color: #FF0000' id='results1'></div></b></p>
              </body>
              </html>[/code]
              Last edited by pbmods; Aug 22 '07, 03:20 PM. Reason: Added CODE Tags.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Fanchun, check your Private Messages (PMs). You've already been warned to start using CODE tags.

                Comment

                • jx2
                  New Member
                  • Feb 2007
                  • 228

                  #9
                  i modified 'Go' function, but still have some problem. the result does not look what i expected.
                  what results did you expect? what results youve got?

                  Comment

                  • fanchun
                    New Member
                    • Aug 2007
                    • 7

                    #10
                    i expected to get some numbers, but i got "NaN". This means there is some problem with the function.

                    Comment

                    • jx2
                      New Member
                      • Feb 2007
                      • 228

                      #11
                      Originally posted by fanchun
                      i expected to get some numbers, but i got "NaN". This means there is some problem with the function.
                      NaN means "not a number"

                      my guess is that S, PARMEST[i] or x[i].value is not a number
                      try to remove one of them at a time to see which one cose problems
                      can you give us examle of PARMEST?
                      you can also try to print values to see whats wrong:
                      [code=javascript]
                      var str="";
                      for (i = 1; i < PARMEST.length; i++)
                      {
                      str += " S="+S+" parmest["+i+"]="+PARMEST[i]+" x.value="+x[i].value;
                      }
                      document.getEle mentById('resul ts1').innerHTML =str;
                      }
                      [/code]
                      that should help to find out whats wrong
                      e.g. one of the values could be undefine

                      regards
                      jx2

                      Comment

                      • fanchun
                        New Member
                        • Aug 2007
                        • 7

                        #12
                        thanks.

                        I checked it using str code. i found the problem is that x.value is empty, it did not take value i put in the text box. it is empty because i did not set value when i use input tag to create text box. i want to input and change it later.

                        the other problem is that at the end of loop, x.value= calculate becausein the last input tag, i specify value='calccula te", actually i want to escape it.

                        do you know how to handle these problems?
                        [code=javascript]
                        PARMEST = [
                        0.7202310101,
                        0.5141934248,
                        -0.450034571,
                        -4.94631E-6,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0];[/code]
                        Last edited by pbmods; Aug 22 '07, 08:39 PM. Reason: Added CODE Tags.

                        Comment

                        • fanchun
                          New Member
                          • Aug 2007
                          • 7

                          #13
                          thanks.

                          I solved it .

                          Regards

                          Comment

                          • jx2
                            New Member
                            • Feb 2007
                            • 228

                            #14
                            Originally posted by fanchun
                            thanks.

                            I solved it .

                            Regards
                            could you tell us what was it?

                            Comment

                            • fanchun
                              New Member
                              • Aug 2007
                              • 7

                              #15
                              I modified the function to solve thoes problems.

                              I put values in text box into an array to solve the first problem.
                              to solve the second problem, I use index i-1 for input values.

                              here is the code for the function

                              [CODE=javascript]
                              function Go()
                              {
                              document.getEle mentById('resul ts1').innerHTML = "";
                              var Selectedvalue = [];
                              var x=document.getE lementsByTagNam e("input");
                              for (i=0; i < x.length; i++)
                              {
                              Selectedvalue.p ush(x[i].value);
                              }
                              var S = PARMEST[0];
                              for (i = 1; i < PARMEST.length; i++)
                              {
                              S = S+PARMEST[i]*Selectedvalue[i-1];
                              var t=Math.exp(S);
                              var p=t/(1+t);
                              }
                              document.getEle mentById('resul ts1').innerHTML =p
                              }
                              [/CODE]

                              Comment

                              Working...