(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.

    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
  2. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #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:
    230
    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 Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #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