Skip to content

Instantly share code, notes, and snippets.

@KDCinfo
Last active April 29, 2017 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KDCinfo/e66482adbc0da297131a6db03a9a6be1 to your computer and use it in GitHub Desktop.
Save KDCinfo/e66482adbc0da297131a6db03a9a6be1 to your computer and use it in GitHub Desktop.
Coding Challenge - Hour Seconds

Coding Challenge - Hour Seconds

Language / Framework

  • Vanilla JavaScript

Attribution

This project derived from: Programming Challenges: [Convert to Seconds]

Project Timeline

Goal

  • Write a function that takes a number of hours and converts it to seconds. Optional: Use the date object to figure out how old you are in seconds.

Stats

  • 2017-04-28 -> 5:53pm - 6:54pm = 1 hour

    • 0.25 hours = 1.25 hours (added the Hours to Seconds conversion)
  • Project status: Successful

  • Project optional: Failed (didn't use a date object)

Overall project took about 1.5 hours to fully complete. Room for improvement.

This Code on CodePen

Project Notes

  • Created self-executing closure just to keep it all contained.
  • I did have to look up the addEventListener syntax (argument order).
  • I found it strange I could not place the console.clear() above the closure.
  • Unsure if making the operation functions static and wrapping in a class would be a good approach?
  • Once finished, I used Google to double check my results.
    • I determined that Google must use 365/12 to account for monthly variations.
    • I plugged this assumption into my code and confirmed.
// This project derived from:
// http://code.startupinacar.com/programming-challenges/
// Programming Challenges: [Convert to Seconds]
// --- --- --- --- --- --- --- --- ---
(function() {
console.clear()
// Make DOM elements available for JavaScript use
let numInputYrs = document.getElementById('num-input-yrs'),
numInputMos = document.getElementById('num-input-mos'),
numSecsInput = document.getElementById('num-secs'),
numInputHrs = document.getElementById('num-input-hrs'),
numSecsInputHrs = document.getElementById('num-secs-hrs')
// Add event listeners for each number input
// Assign appropriate reference function
numInputYrs.addEventListener("change", updateYrsMosSecs)
numInputMos.addEventListener("change", updateYrsMosSecs)
numInputHrs.addEventListener("change", updateHrsSecs)
// Convert hours to Seconds
function updateHrsSecs(e) {
e.preventDefault()
let hrsMins = getMins(parseInt(numInputHrs.value)),
hrsSecs = getSecs(hrsMins)
console.log('hrsMins', hrsMins, 'hrsSecs', hrsSecs)
numSecsInputHrs.value = flattenNum(hrsSecs)
}
// Convert Yrs and Mos to Seconds
function updateYrsMosSecs() {
let numYrs = parseInt(numInputYrs.value),
numMos = getMos(numYrs) + parseInt(numInputMos.value),
numDys = getDys(numMos), // Averaging days (365/12)
numHrs = getHrs(numDys),
numMins = getMins(numHrs),
numSecs = getSecs(numMins)
console.log('numYrs', numYrs, 'numMos', numMos, 'numDys', numDys, 'numHrs', numHrs, 'numMins', numMins, 'numSecs', numSecs)
numSecsInput.value = flattenNum(numSecs)
}
})()
function flattenNum(num) {
return Math.ceil(num)
}
function getMos(num) {
return num * 12;
}
function getDys(num) {
return num * (365/12); // 30.41666666666667;
}
function getHrs(num) {
return num * 24;
}
function getMins(num) {
return num * 60;
}
function getSecs(num) {
return num * 60;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment