Help with a function

Discussion in 'Javascript' started by Linuxmade88, Mar 11, 2020.

  1. Linuxmade88

    Linuxmade88

    Joined:
    Jan 30, 2020
    Messages:
    14
    Likes Received:
    0
    Location:
    Grover Beach
    I stuck trying to figure out how to convert hours and min to seconds inside a fucntion ill post my directions and if someone can help give me a better understanding of the formula id really appreciate it.



    A function that takes hours, minutes, seconds as parameters, and returns the total number of seconds (convert each to seconds, then add them up)


    Code (Text):
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    // javascript code begin
    function hms2s( h, m, s) {
       
        var total = h + m + s ;
         return total ;

    }

    document.write(hms2s(2,3,4) );

    /*A function that takes hours, minutes, seconds as parameters, and returns the total number of seconds (convert each to seconds, then add them up)*/
    // javascript code end
    </script>
    </body>
    </html>
     

    so far the above code is what ive been able to piece together the numbers at the bottom being used to call the function are just to test if the code works. thank you in advance.
     
    Linuxmade88, Mar 11, 2020
    #1
    1. Advertisements

  2. Linuxmade88

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    82
    Likes Received:
    10
    You convert minutes to seconds by multiplying the minutes by 60.
    You can change the hours two way:
    1.) Multiplying the hours by 60 to obtain the number of minutes and then converting that OR
    2.) Simply multiplying hours by 3600 to obtain the number of seconds straight away.

    Code (Text):

    function hms2s( h, m, s) {
           var total = (3600 * h) + (60 * m) + s ;
         return total ;
    }
     
    A big tip FYI
    Don't use document.write() it over writes anything already on the browser.
    Guess it's OK(?) it your stage of learning, BUT it's a bad habit to get into.
    Make a DIV, with an ID, in the HTML body and then write to that using
    document.getElementById().innerHTML or
    document.getElementById().innerText

    Code (Text):

    <!DOCTYPE html>
    <html>
    <head>
      <title>Your Title</title>
    </head>
    <body>
    <div id="wrapper"></div>

    <script>
    function hms2s( h, m, s) {
      var total = (3600 * h) + (60 * m) + s ;
      return total ;
    }
    document.getElementById("wrapper").innerHTML = hms2s( 5, 38, 10);
    </script>
    </body>
    </html>
     
     
    sunfighter, Mar 11, 2020
    #2
    1. Advertisements

  3. Linuxmade88

    Linuxmade88

    Joined:
    Jan 30, 2020
    Messages:
    14
    Likes Received:
    0
    Location:
    Grover Beach
    thank you again for your help dude, are you the only person maintaining on here? either way i appreciate your help alot i wish i had more knowledge of this to help you... last question for this post am i able to use multiple divs on here? so if i made another function i could use a seperate div for that function ?
     
    Linuxmade88, Mar 13, 2020
    #3
  4. Linuxmade88

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    82
    Likes Received:
    10
    Yes of course you can. You could also use the same DIV, all depends on how you want to display the two outputs.
    If you have an idea what the final browser screen will look like for the outputs we can put the JS together.
     
    sunfighter, Mar 13, 2020
    #4
  5. Linuxmade88

    independent

    Joined:
    Nov 23, 2019
    Messages:
    57
    Likes Received:
    1
    mmm,I think ..if this user is requesting that example for a real webpage application,then using.innerHTML might be not suitable for her/him at this stage.
     
    independent, Mar 15, 2020
    #5
  6. Linuxmade88

    Linuxmade88

    Joined:
    Jan 30, 2020
    Messages:
    14
    Likes Received:
    0
    Location:
    Grover Beach
    all so far what we have been doing is creating code in the w3schools sandbox occasionally ill use a text editor like sublime or atom then preview the code in firefow since i cant for the life of me figure out how to use visual studio to easily preview code . since we are learning our teacher doesnt really care about little things you shouldnt do in the code just as long as it works and we learn the subject being showed at hand. but im always trying to improve and i have been trying to take all the advice youve been giving to me . but i have another question , we are starting to learn about loops and im trying to use the <div> you showed me to get the output of this loop, i can only get it to work using document.write am i doing something wrong ?
    Code (Text):
    <!DOCTYPE html>
    <html>
    <body>
    <div id="count"></div>
    <script>
    // javascript code begin

    var counterVar = 0

    do {
        document.getElementById("count").innerHTML= counterVar + " " ;
        counterVar += 1 ;
        }
        while ( counterVar <= 15 )

    // javascript code end
    </script>
    </body>
    </html>
     
     
    Linuxmade88, Mar 27, 2020 at 9:28 PM
    #6
  7. Linuxmade88

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    82
    Likes Received:
    10
    Always end a statement with a semicolon. Sloppy habit to not include them.
    Moved to innerHTML to outside the loop so it prints the final value:
    Code (Text):

    <!DOCTYPE html>
    <html>
    <body>
    <div id="count"></div>
    <script>
    // javascript code begin
    var counterVar = 0;
    do {
        counterVar += 1;
    }
    while ( counterVar <= 15 );
        document.getElementById("count").innerHTML= counterVar;
    // javascript code end
    </script>
    </body>
    </html>
     
     
    sunfighter, Mar 28, 2020 at 4:24 PM
    #7
  8. Linuxmade88

    Linuxmade88

    Joined:
    Jan 30, 2020
    Messages:
    14
    Likes Received:
    0
    Location:
    Grover Beach
    Im gonna try playing around with it to figure it out hopefully but I need it to display the output as the whole count from 0 - 15. is there no way to do that?
     
    Linuxmade88, Mar 28, 2020 at 4:54 PM
    #8
  9. Linuxmade88

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    82
    Likes Received:
    10
    Oh :D My bad.
    Code (Text):

    <!DOCTYPE html>
    <html>
    <body>
    <div id="count"></div>
    <script>
    // javascript code begin
    var text = ""
    var counterVar = 0;
    do {
        text += counterVar + "<br>";
        counterVar++;
    }while (counterVar <= 15);
    document.getElementById("count").innerHTML= text;
    // javascript code end
    </script>
    </body>
    </html>
     
     
    sunfighter, Mar 29, 2020 at 1:52 PM
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.