473,473 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How To Build A Formula Using Javascript?

7 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. var factor=["intercept", "x1","x2","x3",x4","x5"]
parm.js is also an array, having coresponding parameters to those factors in factor.js file. it looks like
Expand|Select|Wrap|Line Numbers
  1. var parm=["0.5", "0.1", "-0.2","0","1","0.3"]
I want to build an regression formula based on the factors and parameters in these two js file. for example,
Expand|Select|Wrap|Line Numbers
  1. y= 0.5+x1*0.1+x2*(-0.2)+x3*0+x4*1+x5*0.3
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
Aug 20 '07 #1
15 5792
gits
5,390 Recognized Expert Moderator Expert
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
Aug 20 '07 #2
pbmods
5,821 Recognized Expert Expert
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.
Aug 20 '07 #3
fanchun
7 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <html>    
  2.  <head>    
  3.  <style type='text/css'>* { font-size: 10pt;}</style>    
  4.  <script type='text/javascript' src='PARMEST.js'></script>    
Expand|Select|Wrap|Line Numbers
  1.  <script type='text/javascript'>    
  2.  function Go()    
  3.  {    
  4.    document.getElementById('results1').innerHTML = "";    
  5.     var Selectedvalue = [];
  6.     for (i=0; i < document.getElementByType("text").length; i++)
  7.     {
  8.         Selectedvalue.push(document.getElementByType("text").value);
  9.     }    
  10.    var S = 0;    
  11.    for (i = 0; i < PARMEST.length; i++)    
  12.    {    
  13.        S = S+PARMEST[i]*Selectedvalue[i];    
  14.    }    
  15.   document.getElementById('results1').innerHTML=S    
  16.  }    
Expand|Select|Wrap|Line Numbers
  1.  </script>    
  2.  </head>    
  3. <FORM NAME="form1">
  4. EFFECT <br>
  5. <br>
  6. legal_dep_other2 : <INPUT TYPE='text' NAME='legal_dep_other2 '>
  7. <br>
  8. year_in_school : <INPUT TYPE='text' NAME='year_in_school '>
  9. <br>
  10. total_income : <INPUT TYPE='text' NAME='total_income '>
  11. <br>
  12. age : <INPUT TYPE='text' NAME='age '>
  13. <br>
  14. agi : <INPUT TYPE='text' NAME='agi '>
  15. <br>
  16. father_highest_grade_level2 : <INPUT TYPE='text' NAME='father_highest_grade_level2 '>
  17. <br>
  18. fm_sc : <INPUT TYPE='text' NAME='fm_sc '>
  19. <br>
  20. income : <INPUT TYPE='text' NAME='income '>
  21. <br>
  22. invest_net_worth : <INPUT TYPE='text' NAME='invest_net_worth '>
  23. <br>
  24. marital_status21 : <INPUT TYPE='text' NAME='marital_status21 '>
  25. <br>
  26. marital_status22 : <INPUT TYPE='text' NAME='marital_status22 '>
  27. <br>
  28. mother_highest_grade_level2 : <INPUT TYPE='text' NAME='mother_highest_grade_level2 '>
  29. <br>
  30. num_in_college : <INPUT TYPE='text' NAME='num_in_college '>
  31. <br>
  32. num_in_family : <INPUT TYPE='text' NAME='num_in_family '>
  33. <br>
  34. Q_KEY : <INPUT TYPE='text' NAME='Q_KEY '>
  35. <br>
  36. spouse_income : <INPUT TYPE='text' NAME='spouse_income '>
  37. <br>
  38. veteran0 : <INPUT TYPE='text' NAME='veteran0 '>
  39. <br>
  40. <td valign='bottom'><INPUT TYPE='button' VALUE='Calculate'       onClick='Go()'></td>
  41. </FORM>
  42.  <p><b>Actual Score <div style= 'color: #FF0000' id='results1'></div></b></p>    
  43.  </body>    
  44.  </html>
Aug 21 '07 #4
pbmods
5,821 Recognized Expert Expert
Merged duplicate threads.
Aug 21 '07 #5
jx2
228 New Member
Expand|Select|Wrap|Line Numbers
  1. x=document.getElementsByTagName("input");
  2.  
  3. //and you can use it like that:
  4. myValue = x[1].value;
  5. myName = x[1].name;
  6.  
  7. //or
  8. myValue = x['myInput'].value;
  9.  
regards
jx2
Aug 21 '07 #6
fanchun
7 New Member
thanks.

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

here is the whole javascript code:
Expand|Select|Wrap|Line Numbers
  1.  <html>    
  2.  <head>    
  3.  <style type='text/css'>* { font-size: 10pt;}</style>    
  4.  <script type='text/javascript' src='PARMEST.js'></script>    
  5.  <script type='text/javascript'>    
  6.  function Go()    
  7.  {    
  8.    document.getElementById('results1').innerHTML = "";    
  9. var x=document.getElementsByTagName("input");
  10.  var S = PARMEST[0];    
  11.    for (i = 1; i < PARMEST.length; i++)    
  12.    {    
  13.        S = S+PARMEST[i]*x[i].value;    
  14.    }    
  15.   document.getElementById('results1').innerHTML=S    
  16.  }    
  17.  </script>    
  18.  </head>    
  19. <FORM NAME="form1">
  20. EFFECT <br>
  21. <br>
  22. legal_dep_other2 : <INPUT TYPE='text' NAME='legal_dep_other2 '>
  23. <br>
  24. year_in_school : <INPUT TYPE='text' NAME='year_in_school '>
  25. <br>
  26. total_income : <INPUT TYPE='text' NAME='total_income '>
  27. <br>
  28. age : <INPUT TYPE='text' NAME='age '>
  29. <br>
  30. agi : <INPUT TYPE='text' NAME='agi '>
  31. <br>
  32. father_highest_grade_level2 : <INPUT TYPE='text' NAME='father_highest_grade_level2 '>
  33. <br>
  34. fm_sc : <INPUT TYPE='text' NAME='fm_sc '>
  35. <br>
  36. income : <INPUT TYPE='text' NAME='income '>
  37. <br>
  38. invest_net_worth : <INPUT TYPE='text' NAME='invest_net_worth '>
  39. <br>
  40. marital_status21 : <INPUT TYPE='text' NAME='marital_status21 '>
  41. <br>
  42. marital_status22 : <INPUT TYPE='text' NAME='marital_status22 '>
  43. <br>
  44. mother_highest_grade_level2 : <INPUT TYPE='text' NAME='mother_highest_grade_level2 '>
  45. <br>
  46. num_in_college : <INPUT TYPE='text' NAME='num_in_college '>
  47. <br>
  48. num_in_family : <INPUT TYPE='text' NAME='num_in_family '>
  49. <br>
  50. Q_KEY : <INPUT TYPE='text' NAME='Q_KEY '>
  51. <br>
  52. spouse_income : <INPUT TYPE='text' NAME='spouse_income '>
  53. <br>
  54. veteran0 : <INPUT TYPE='text' NAME='veteran0 '>
  55. <br>
  56. <td valign='bottom'><INPUT TYPE='button' VALUE='Calculate'       onClick='Go()'></td>
  57. </FORM>
  58.  <p><b>Actual Score <div style= 'color: #FF0000' id='results1'></div></b></p>    
  59.  </body>    
  60.  </html>
Aug 22 '07 #7
pbmods
5,821 Recognized Expert Expert
Fanchun, check your Private Messages (PMs). You've already been warned to start using CODE tags.
Aug 22 '07 #8
jx2
228 New Member
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?
Aug 22 '07 #9
fanchun
7 New Member
i expected to get some numbers, but i got "NaN". This means there is some problem with the function.
Aug 22 '07 #10
jx2
228 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.  var str="";
  2.  for (i = 1; i < PARMEST.length; i++)    
  3.    {    
  4.        str += " S="+S+" parmest["+i+"]="+PARMEST[i]+" x.value="+x[i].value;    
  5.    }    
  6.   document.getElementById('results1').innerHTML=str;   
  7.  }   
  8.  
that should help to find out whats wrong
e.g. one of the values could be undefine

regards
jx2
Aug 22 '07 #11
fanchun
7 New Member
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='calcculate", actually i want to escape it.

do you know how to handle these problems?
Expand|Select|Wrap|Line Numbers
  1. PARMEST  = [
  2. 0.7202310101,
  3. 0.5141934248,
  4. -0.450034571,
  5. -4.94631E-6,
  6. 0,
  7. 0,
  8. 0,
  9. 0,
  10. 0,
  11. 0,
  12. 0,
  13. 0,
  14. 0,
  15. 0,
  16. 0,
  17. 0,
  18. 0,
  19. 0];
Aug 22 '07 #12
fanchun
7 New Member
thanks.

I solved it .

Regards
Aug 22 '07 #13
jx2
228 New Member
thanks.

I solved it .

Regards
could you tell us what was it?
Aug 22 '07 #14
fanchun
7 New Member
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

Expand|Select|Wrap|Line Numbers
  1. function Go()    
  2.  {    
  3.    document.getElementById('results1').innerHTML = "";    
  4.  var Selectedvalue = [];    
  5. var x=document.getElementsByTagName("input");
  6.     for (i=0; i < x.length; i++)
  7.     {
  8.         Selectedvalue.push(x[i].value);
  9.     }    
  10.    var S = PARMEST[0];    
  11.    for (i = 1; i < PARMEST.length; i++)    
  12.    {    
  13.        S = S+PARMEST[i]*Selectedvalue[i-1];    
  14.     var t=Math.exp(S);    
  15.     var p=t/(1+t);    
  16.    }    
  17.   document.getElementById('results1').innerHTML=p    
  18.  }     
  19.  
Aug 22 '07 #15
pbmods
5,821 Recognized Expert Expert
Heya, Fanchun.

Thanks for posting the solution!

And thanks for using CODE tags, too!

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 22 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Eugene | last post by:
Hi all, I have a page that uses JavaScript to continuously reload an image (Client-Pull method). Using Netscape v7.0 on Windows NT 4, Service Pack 6a, the images reload fine, but using...
3
by: Stan Sainte-Rose | last post by:
Hi, How to eval a formula on the fly ? I have a table where I store some formula like "(5+9)/2" Each number reffers to a row so that gives (row(5)+row(9))/row(2) I think if someone could...
4
by: Wade | last post by:
Hi guys, I am building a string in code that is a mathematical formula, based on a lot of criteria specified by the user. Once the formula is complete, I want to evaluate it to get a result. ...
3
by: Mike | last post by:
Hi, I have three tables in the following structure (simplified): Table 1: Containing the customers ------------------------------------------------- create table Customers ( int...
8
by: grahamhow424 | last post by:
Hi I am trying to figure out how to duplicate a, financial, calculation that uses the caret, Exponentiation. Here's the formula... A = 0.0755 B = 34 C = 50000
10
by: Ron | last post by:
I've got this simple program that will not build or run, any idea what I am doing wrong? Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
3
by: VJ | last post by:
We have a form where there is a like 10-15 fields and it performs a length formula calucation each time field value changes. Using validate event is not a option as validate wont fire on Menu click...
1
by: JHaworth | last post by:
Hi, Please could someone point me in the right direction with a formula I am trying to create which will enable me to calculate a Net IRR for a series of data? My Problem: •My formula in...
8
by: K Viltersten | last post by:
I understand that the new versions of MS Word, MS Excel etc. allow for creation of a document using XML tags (the technique or format is called OOXML, i think). I imagine that the idea behind...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.