Login | Register | FAQ
Anonymous

Preforming basic maths functions

+ Post a reply

21 posts 1, 2, 3


Preforming basic maths functions

by nicci5 » Tue Jul 17, 2012 9:56 am

hi i am trying to set a form so that it preforms basic maths functions when certain boxes are filled.
Time Boxes 1-4 are filled in by the user in seconds ie 100,500,600,400
Box a adds the time boxes up which i have dome using value is box.
now the bit i really cant do !!!
box b needs to divide 3600 by the total of box a (which can vary)
Box c needs to divide 2700 by the total of box a
box d needs to divide 25200 by the total of box a

not sure if that makes any sense i am a total novice !!
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am

Re: Preforming basic maths functions

by nicci5 » Tue Jul 17, 2012 10:35 am

small update this is what i have so far
var result=0;
var secondnumber=("1 PERSON CELLCYCLE TIME");
var firstnumber=3600;
result=firstnumber/secondnumber;
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am

Re: Preforming basic maths functions

by atik » Tue Jul 17, 2012 11:24 am

Not sure i have understood fully, but here is what i understood- there are four boxes for time input (box 1 - 4) and four other boxes ( box a - d). Box a is sum of box 1-4 and you are able to get this value. If thats the case , then you already have the value of box 1- 4 added and put the value in a variable. Now box b needs to divide 3600 by the total of box a. Since you already have the value of a in the previous step by adding those 4 boxes (1-4) and as i told you to store the total value in a variable, then for box b, all you need to divide 3600 by the variable holding the value of box a. Then in which step you are failing?
User avatar

atik

  • Posts: 472
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: Preforming basic maths functions

by nicci5 » Tue Jul 17, 2012 12:11 pm

Boxes 1-4 will be filled in by another user , so the total in box a will always be different i wont have the total at the time i want to set up a template form so that it always does the calculation when they enter the seconds for each task.
if i had the total i could use 3600/1300 etc but i dont have the total but have the number i need the total to divide by
3600/total of box a
2700/total of box a
25200/total of box a

i think i may be over thinking it but this is the first time i have been asked to do this and i really dont want to look as stupid as i feel to my boss !!!!!
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am

Re: Preforming basic maths functions

by atik » Tue Jul 17, 2012 12:36 pm

Here is the code i wrote for you just for doing your mentioned task -
Code: Select all
<script type="text/javascript">
   function calculate_result(sum) {
      var n1 = document.getElementById("number1").value;
      var n2 = document.getElementById("number2").value;
      var n3 = document.getElementById("number3").value;
      var n4 = document.getElementById("number4").value;
      var sum = Number(n1) + Number(n2) + Number(n3) + Number(n4);
      b = sum / 10.0;
      document.getElementById("a").value = sum;
      document.getElementById("b").value = 3600/sum;
      document.getElementById("c").value = 2700/sum;
      document.getElementById("d").value = 25200/sum;
   }
</script>

<form>
   1st Number:<input type="text" name="number1" id="number1"/><br/>
   2nd Number:<input type="text" name="number2" id="number2"/><br/>
   3rd Number:<input type="text" name="number3" id="number3"/><br/>
   4th Number:<input type="text" name="number4" id="number4"/><br/>
   <input type="button" value="Calculate" onclick="calculate_result();"/><br/>
   a:<input type="text" name="a" id="a"/><br/>
   b:<input type="text" name="b" id="b"/><br/>
   c:<input type="text" name="c" id="c"/><br/>
   d:<input type="text" name="d" id="d"/>
</form>


Basically the idea is to use a function and trigger the function when the user will submit the inputs. All the computations are inside the function. If you need any clarification about the code, then feel free to ask.
User avatar

atik

  • Posts: 472
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: Preforming basic maths functions

by nicci5 » Thu Jul 19, 2012 12:27 pm

hi

thanks for the help , when i have entered the script it come up with an error ? var n2 = document.getElementById("TIME_2").value;
Syntax error 3:at line 4


also excuse me for being very dim but does all the script go in one text box or does it split to enable each box to work ?

thanks again

nicci
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am

Re: Preforming basic maths functions

by atik » Thu Jul 19, 2012 12:33 pm

nicci5 wrote:hi

thanks for the help , when i have entered the script it come up with an error ? var n2 = document.getElementById("TIME_2").value;
Syntax error 3:at line 4


also excuse me for being very dim but does all the script go in one text box or does it split to enable each box to work ?

thanks again

nicci

I think you have messed up something, otherwise you shouldn't have get any error. Can you post here exactly what code you have tried? Also it handles all 4 cases , not one.
User avatar

atik

  • Posts: 472
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: Preforming basic maths functions

by nicci5 » Thu Jul 19, 2012 12:54 pm

hi i copied and pasted your script so not sure what could of gone wrong ?
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am

Re: Preforming basic maths functions

by atik » Thu Jul 19, 2012 12:58 pm

nicci5 wrote:hi
thanks for the help , when i have entered the script it come up with an error ? var n2 = document.getElementById("TIME_2").value;
Syntax error 3:at line 4

thanks again

nicci

Not exactly copy paste i think, because in your last message here you got syntax error, it seems you have changed the id value of the form field. Thats why i was willing to see if you have changed that id value to other places also.
User avatar

atik

  • Posts: 472
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: Preforming basic maths functions

by nicci5 » Thu Jul 19, 2012 1:01 pm

hi

i changed the names in some places as i thought they would have to be called what they ate in the form ie your number 1 field is called TIME on my form , if i change the names on my form to match yours do you think that would work ?

Nicci
User avatar

nicci5

  • Posts: 12
  • Joined: Tue Jul 17, 2012 9:51 am


+ Post a reply

1, 2, 3