cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">
  <h1>Figure Out When To Go Home.</h1>
  <p>
  Calculates time input + 8.58 hours.
</p>
<input type="text" id="time" placeholder="time you came in">
<div id="results"></div>
</div>


            
          
!
            
              body {
  font-family: sans-serif;
}

input {
  text-align: center;
  font-size: 1.2rem;
  padding: 8px;
  border-radius: 5px;
  border: solid .5px #ccc;
}

input:focus{
  border-radius: 5px;
  border: solid 2px #6495ED77;
}

.container {
  max-width: 800px;
  margin: auto;
  text-align: center;
}
            
          
!
            
              const time = document.getElementById('time');
const results = document.getElementById('results');

// whenever there a keyup in the "time" input, the calcTime function is triggered.
time.addEventListener("keyup", calcTime);

function calcTime(){
  // contains the value in the input box
    const timeInString = time.value;
  // contains timeInString value as an object with an hours and minutes property
    const timeObj = splitHrsNMins(timeInString, ":");
  // turn the hour into an int
    const hrs = parseInt(timeObj.hours);
  // convert the minutes into decimal of hour
    const minsToDec = parseInt(timeObj.minutes) / 60;
  // calculate when I can leave - 8.58 hrs later
    const leavTimeDec = hrs + minsToDec + 8.58;
  // contains leavTimeDec value as an object with an hours and minutes property
    const leavTimeObj = splitHrsNMins(leavTimeDec, ".");
  // contains the hour that I can leave
    const leavHr = parseInt(leavTimeObj.hours);
  // contains the minute in minute form (not decimal) - rounds to nearest minute.
    const leavTimeMins = Math.round((leavTimeDec - leavHr) * 60);
    
    
// template to output results
    results.innerHTML = `<p>hours: ${timeObj.hours}<br>minutes: ${timeObj.minutes}</p>
                        <p>time in decimal: ${hrs + minsToDec}</p>
                        <p>leave time in decimal: ${leavTimeDec}</p>
                        <h3>Leave at ${leavHr < 24 ? leavHr : leavHr - 24}:${leavTimeMins > 9 ? leavTimeMins : "0" + leavTimeMins}</h3>`;
}

// function to split a time string or a float representing the time as a decimal into an object with an hours and minutes property.
// the object values are strings
//time represents the string or float, separator represents the charachter separating between the hours and minutes.
function splitHrsNMins(time, separator){
    let timeStr = time.toString();
    let timeArr = timeStr.split(separator);
    timeObject = {
        "hours": timeArr[0],
        "minutes": timeArr[1]
    };
    return timeObject;

}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console