Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: simple java help (homework)

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple java help (homework)

    Cant figure out why this is not working any help would be great.

    function bulletf2()
    {
    var bulletspeed = 1325;
    var bulletDistance;
    var bulletTime;
    var bulletDistance = document.getElementById('InID2');
    bulletTime= bulletDistance/ bulletspeed;
    var bulletTime = document.getElementById('OutID2');

    }



    //-->
    </script>
    </head>
    <body bgcolor="#C0C000">
    <H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
    <!-- ---------------------------------------------------------------------- -->
    <!-- Copy of your java script code for show! -->
    <!-- ---------------------------------------------------------------------- -->
    <P style="color: #0000ff; border=2">a view of your javascript code...</P>
    <textarea style="background-color: #ffffcc;" cols=80 rows=8>
    </textarea><hr/>
    <P style="color: #800080">2. A bullet travels at 1325 meter's per second.
    Write a program that will input the Distance and Show the Time.</P>
    <H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2()" /></H3>
    <H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>

  2. #2
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    dude it's for a mid-term, I suggest you google the hell out of javascript.

    I'll help you do this, but don't shoot yourself in the foot (pun!).
    document.write is document.wrong

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    1) You have to select the value, not the element
    2) You have to assign the elements value bulletTime, not assign bulletTime's value to the element.

    There is also a much easier way to do this which I have already done... I don't know if I should give it to you considering it's homework.
    Jeremy | jfein.net

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Your code doesn't work...
    You're missing a slash in it's.. should be: it\'s.
    (btw: taking out the document.write, the code doesn't work. )
    Jeremy | jfein.net

  5. #5
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Your code doesn't work...
    You're missing a slash in it's.. should be: it\'s.
    fixed it, thanx.
    document.write is document.wrong

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Your code doesn't work though... Were you trying to make it work? Or help him?
    Jeremy | jfein.net

  7. #7
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by collicott View Post
    Cant figure out why this is not working any help would be great.

    function bulletf2()
    {
    var bulletspeed = 1325;
    var bulletDistance;
    var bulletTime;
    var bulletDistance = document.getElementById('InID2');
    bulletTime= bulletDistance/ bulletspeed;
    var bulletTime = document.getElementById('OutID2');

    }



    //-->
    </script>
    </head>
    <body bgcolor="#C0C000">
    <H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
    <!-- ---------------------------------------------------------------------- -->
    <!-- Copy of your java script code for show! -->
    <!-- ---------------------------------------------------------------------- -->
    <P style="color: #0000ff; border=2">a view of your javascript code...</P>
    <textarea style="background-color: #ffffcc;" cols=80 rows=8>
    </textarea><hr/>
    <P style="color: #800080">2. A bullet travels at 1325 meter's per second.
    Write a program that will input the Distance and Show the Time.</P>
    <H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2()" /></H3>
    <H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>

    here's the deal...
    Let's say you have an input element right?
    <input id="ninja" name="ninja" value="el">

    ok, first off, if we want to replace this element's value, we use this
    document.getElementById('ninja').value - "new text";

    what we are doing is targetting the element based on the id="ninja" part of <input id="ninja" name="ninja" value="el"> .
    what was wrong in your formula was that you were trying to divide raw string values.... first you have to wrap each value, like - parseFloat(bulletDistance) / parseFloat(the otherthing).

    I think some of your values set for variables were absolutely blank as well...
    Assign them like this ----
    var BulletThingy = document.getElementById("whateverelement").value;

    now to change the value of an element it's the other way...
    document.getElementById("whateverelement").value = "7";

    parseFloat("numericvalue") tells javascript that the values are to be treated as mathematical (correct me if I am wrong guys).
    document.write is document.wrong

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ehh... Almost...
    Here was trying to divide raw html objects... In javascript, it does:

    Code:
    [HtmlInputObject] / 1325
    Or something like that.

    (your code doesn't work!)
    Jeremy | jfein.net

  9. #9
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Your code doesn't work though... Were you trying to make it work? Or help him?

    I screwed it up when I was trying to be witty.... damn you pride! Foiled again!

    Code:
    <script type="text/javascript">
    function bulletf2(value)
    {
    var bulletspeed = 1325;
    var bulletDistance = value;
    var bulletTime;
    var bulletDistance = document.getElementById('InID2').value;
    bulletTime= parseFloat(bulletDistance)/ parseFloat(bulletspeed);
    document.getElementById('OutID2').value = bulletTime;
    } 
    //-->
    </script>
    </head>
    <body bgcolor="#C0C000">
    <H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
    <!-- ---------------------------------------------------------------------- -->
    <!-- Copy of your java script code for show! -->
    <!-- ---------------------------------------------------------------------- -->
    <P style="color: #0000ff; border=2">a view of your javascript code...</P>
    <textarea style="background-color: #ffffcc;" cols=80 rows=8>
    </textarea><hr/>
    <P style="color: #800080">2. A bullet travels at 1325 meter's per second. 
    Write a program that will input the Distance and Show the Time.</P>
    <H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2(this.value)" /></H3>
    <H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>
    document.write is document.wrong

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Well... if were giving answers:
    HTML Code:
    <script type="text/javascript">
    var calculation = {
      metersps: 1325,
      calcTime: function(me, outputEl){
        me.value = parseFloat(me.value);
        outputEl.value = parseFloat(me.value) / this.metersps;
      },
      calcDistance: function(me, outputEl){
        me.value = parseFloat(me.value);
        outputEl.value = parseFloat(me.value) * this.metersps;
      }
    };
    </script>
    Distance:<br />
    <input type="text" name="distance" id="inputDist" onkeyup="calculation.calcTime(this, document.getElementById('inputTime'));" />
    <br /><br />
    Time:<br />
    <input type="text" name="time" id="inputTime" onkeyup="calculation.calcDistance(this, document.getElementById('inputDist'));" />
    <br />
    That one can go both ways... You can type the distance or time.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •