+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: STUDENT - Javascript problem

  1. #1
    goldy300's Avatar
    goldy300 is offline x10Hosting Member goldy300 is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    Australia
    Posts
    33

    Question STUDENT - Javascript problem

    Doing IT diploma but I'm stuck on an exercise for Java script.

    Here it is:

    The number of calories burned per hour by bicycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1Kg for each 1600 calories burned. Create a web application that allows the User to input the number of hours spent at each activity and then calculates the number of Kilograms burnt off.

  2. #2
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: STUDENT - Javascript problem

    Edit: Skip to next post for your solution ;)

    Are you completely stuck or is there just a part that is unclear to you? I know virtually nothing about JavaScript but I could give ideas and if you can combine it to PHP it's gonna be even easier.

    Very simply put, I would make an html form with a dropdown list of the three activities OR three text inputs each representing one activity (so the user can check the three of them at once). Then your onSubmit property would call a calculation function that would use the form input's name to identify which formula it must use to get the total amount of calories (multiplying the number of hours per 200, 475 or 275)and complete it with translating the calories to kilograms.

    You could then use a second form, with a text input, that would display the number of kilograms burnt. To do this, you just have to do like forums that display a number of characters allowed vs. characters already used. Which, if I remember correctly, works by giving an id to the text input and making its value change with a JS function (something like this.form.input.value = "your kg amount calculated above" but I'm very approximate on this).

    I'm just saying this to give you ideas though, I won't be able to help you much when it comes to actual coding.
    Hope that helps.
    Last edited by Salvatos; 10-03-2008 at 01:21 AM. Reason: Dickey gave the script ^^

  3. #3
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: STUDENT - Javascript problem

    This is something I come up with. If this helps I don't mind reps and credit donations.
    HTML Code:
    <html>
      <head>
        <script>
          function compute()
          {
            var input = document.getElementById('hours').value;
            var activity = document.getElementById('activity').value;
            calories = input * activity;
            out = document.getElementById('KG');
            out.value = calories/1600 + " KG";
            if (out.value>100) 
            {
              alert('Chances are you are dead by now. So wipe off that silly grin from your face!');
            }
            document.getElementById('message').style.display='block';        
          }
        </script>
    
      </head>
      <body>
        <form name='form1'>
          What form of activity did you do?
          <select name='activity'>
            <option value='200'>Cycling</option>
            <option value='475'>Jogging</option>
            <option value='275'>Swimming</option>
            <option value='2000000'>Harcore Sex</option>
          </select>
          <br />
          How many hours did you do it?
          <input type='text' name='hours' />
          <br />
          <input type='button' value='Compute' onclick='javascript:compute()' />
          <div id='message' style='display:none'>
            Congratulations you lost: 
            <input type='text' name='KG' />
            <br />
            <input type='reset' value='Again?' onclick="javascript:document.getElementById('message').style.display='none'" />
          </div>
        </form>
      </body>
    </html>
    if you are using firefox
    HTML Code:
    <html>
      <head>
        <script>
          function compute()
          {
            input = document.getElementById('hours');
            input = input.value;
            var activity = document.getElementById('activity').value;
            calories = input * activity;
            KG=calories/1600;
            out = document.getElementById('KG');
            out.value = calories/1600 + " KG";
            if (KG > 100) 
            {
              alert('Chances are you are dead by now. So wipe off that silly grin from your face!');
            }
            document.getElementById('message').style.display='block';        
          }
        </script>
    
      </head>
      <body>
        <form name='form1'>
          What form of activity did you do?
          <select name='activity' id='activity'>
            <option value='200'>Cycling</option>
            <option value='475'>Jogging</option>
            <option value='275'>Swimming</option>
            <option value='2000000'>Harcore Sex</option>
          </select>
          <br />
          How many hours did you do it?
          <input type='text' name='hours' id='hours' />
          <br />
          <input type='button' value='Compute' onclick='javascript:compute()' />
          <div id='message' style='display:none'>
            Congratulations you lost: 
            <input type='text' name='KG' id='KG' />
            <br />
            <input type='reset' value='Again?' onclick="javascript:document.getElementById('message').style.display='none'" />
          </div>
        </form>
      </body>
    </html>
    Last edited by dickey; 10-03-2008 at 01:18 AM. Reason: does not work in firefox only ie. but that is settled now.

  4. #4
    Nahid_hossain is offline x10Hosting Member Nahid_hossain is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Dhaka, Bangladesh
    Posts
    28

    Re: STUDENT - Javascript problem

    Try this simple code:

    HTML Code:
    <script type="text/javascript">
    $byc=prompt("How many hours have you been bycycling?");
    $jog=prompt("How many hours have you been jogging?");
    $swm=prompt("How many hours have you been swimming?");
    $loses=($byc*200+$jog*475+$swm*275)/1600;
    $rloses=$loses.toFixed(2);
    document.write("You have burnt "+$rloses+" Kilograms");
    </script>
    Last edited by Nahid_hossain; 10-03-2008 at 01:34 AM.

  5. #5
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: STUDENT - Javascript problem

    All very well, but why should we do your homework for you?
    If you had at least made a start, and gone wrong, then perhaps I'd be more helpful.
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  6. #6
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: STUDENT - Javascript problem

    Quote Originally Posted by Nahid_hossain View Post
    Try this simple code:

    HTML Code:
    <script type="text/javascript">
    $byc=prompt("How many hours have you been bycycling?");
    $jog=prompt("How many hours have you been jogging?");
    $swm=prompt("How many hours have you been swimming?");
    $loses=($byc*200+$jog*475+$swm*275)/1600;
    $rloses=$loses.toFixed(2);
    document.write("You have burnt "+$rloses+" Kilograms");
    </script>
    It's good and simple. But I think if I was his instructor I would notice that it is not his work.

    THis is a simple problem and he was stuck and it seems he didn't even have a code to start with.

  7. #7
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: STUDENT - Javascript problem

    Also, I've never seen javascript that uses php variables!

    Goldy would have to explain how the code works, so the above code is probably not much use after all (but very neat -all credit to Nahid)
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  8. #8
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: STUDENT - Javascript problem

    Quote Originally Posted by mattura View Post
    All very well, but why should we do your homework for you?
    If you had at least made a start, and gone wrong, then perhaps I'd be more helpful.
    Well he didn't ask to make it for him, he just said he was stuck ;)

    As I don't know much JS myself but am still fairly interested, is Nahid's code complete or would one have to add some html to provide the entries? I don't know how the prompt() function works, so... Just curiosity ^^

  9. #9
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Question Re: STUDENT - Javascript problem

    Quote Originally Posted by mattura View Post
    All very well, but why should we do your homework for you?
    If you had at least made a start, and gone wrong, then perhaps I'd be more helpful.
    Not do homework for me... it was as one other guy said, I was unclear and needed to understand it. Most people have been heaps helpful... thanx everyone else.
    Edit:
    I can see the variations of doing this but I was thinking of the user adding values to all separate fields and then calculating total kilos lost after that. As I said I'm very vague on this so this may look stupid if it's wrong...

    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>

    </HEAD>

    <script>
    function compute()
    {
    document.form1.sum.value=(document.form1.cyc.value )* 200/1600;

    document.form1.sum.value=(document.form1.jog.value )* 475/1600;

    document.form1.sum.value=(document.form1.swim.valu e)* 275/1600;
    }
    </script>

    </head>
    <body>
    <form name='form1'>
    <b><font face="arial">How many kilo's have you lost?</font></b><p>
    <font face="arial">
    Cycling: <input name="cyc" type="text" size="4" onChange="compute()"><br>
    Jogging: <input name="jog" type="text" size="4" onChange="compute()"><br>
    Swimming:<input name="swim" type="text" size="4" onChange="compute()"><br>
    You have lost: <input name="sum" type="text" size="7" value="0"> Kilograms.<br>
    <input type="button" name="" value="Calculate"> &nbsp; <input type="reset" value="Reset">
    </font>
    </div>
    </form>
    </body>
    </html>

    Let me know... thanx!
    Last edited by goldy30; 10-03-2008 at 08:01 AM. Reason: Automerged Doublepost

  10. #10
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: STUDENT - Javascript problem

    Watch the space on this line:
    document.form1.sum.value=(document.form1.swim.valu e)* 275/1600;

    Also "How many kilo's have you lost?" would be "How many kilos have you lost?" and you don't really need to close that first font tag since you're opening an identical one right after.

    Now about the JS proper, there is the reset button which is not going to do anything.
    Apart from that, I don't think your total is gonna come up right.
    Again, I'm not an expert, but I'm pretty sure your sum is gonna end up to be equal to the amount related to swimming only, since you're overwriting the same variable on each line.
    You should probably make three different variables and have a fourth combining all three, and this fourth one would be the one displayed.

    Also, your submit (Calculate) button does not do anything since the sum updates as soon as the values are changed.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  2. JavaScript problem
    By anuj_web in forum Programming Help
    Replies: 25
    Last Post: 04-24-2008, 05:15 AM
  3. DB number problem
    By lionheart8 in forum Free Hosting
    Replies: 5
    Last Post: 04-08-2008, 08:26 AM
  4. Problem with ad code (includes?)
    By Salvatos in forum Free Hosting
    Replies: 10
    Last Post: 12-12-2006, 04:16 PM
  5. Ad code problem!
    By Akkarin in forum Free Hosting
    Replies: 8
    Last Post: 08-29-2005, 10:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers