1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

(Javascript) Function that takes 180+variable\3 the answer is in hours and minutes.

Discussion in 'JavaScript' started by Mnsoldier5, Feb 24, 2019.

  1. #1
    Hello I need some assistance in writing this code out, basically I will take a user inputted number divided by 3 + 180 then the output will be in hours and minutes..example 72 would be 1 hour 12 minutes.
    SEMrush
    Here's my current code but I can't get it to work.

    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var pets = prompt ("How many pets do you own?");
    Function GetTime() {
    Math.floor(180 + pets/60) +" Hour "+ pets%60 + " Minutes.";
    }
    document.write GetTime;
    </script>
    
    </body>
    </html>
    Code (markup):
     
    Mnsoldier5, Feb 24, 2019 IP
    SEMrush
  2. mmerlinn

    mmerlinn Notable Member

    Messages:
    2,242
    Likes Received:
    283
    Best Answers:
    6
    Trophy Points:
    260
    #2
    1) It looks like your code is producing 181 hours and 12 minutes, which is not even close to what you are looking for.
    2) I do not see anywhere that you are dividing by 3
    3) Your example will never produce the results you are looking for

    pets = 72/3 + 180
    INT(pets)/60 = 3 hours
    MOD(pets, 60) = 24 minutes
     
    mmerlinn, Mar 2, 2019 IP
  3. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #3
    If you want to convert minutes to hours and minutes, you can use something like this:

    function convert_time(number)
    {
      var hours = Math.floor(number / 60); 
      var minutes = number % 60;
      return hours + ":" + minutes;        
    }
    
    Code (JavaScript):
     
    RoseHosting, Mar 3, 2019 IP
  4. mmerlinn

    mmerlinn Notable Member

    Messages:
    2,242
    Likes Received:
    283
    Best Answers:
    6
    Trophy Points:
    260
    #4
    Duh, didn't you READ what the OP said? That is the formula he is using! He needs help with the INPUTS, not the FORMULA.
     
    mmerlinn, Mar 3, 2019 IP