Skip to content

Instantly share code, notes, and snippets.

@dawidjaniga
Created October 13, 2019 10:30
Show Gist options
  • Save dawidjaniga/1fa9a13d7b734e73687cb733db8210bd to your computer and use it in GitHub Desktop.
Save dawidjaniga/1fa9a13d7b734e73687cb733db8210bd to your computer and use it in GitHub Desktop.
class LearingHoursCounter {
    constructor({learningHoursPerDay}) {
        this.learningHoursPerDay = learningHoursPerDay
    }
    sumCoursesTime () {
      const elements = [...document.querySelectorAll('.meta')].map(
        item => item.firstChild.data
      )
     
      let hoursSum = 0
      let minutesSum = 0
      const numbersRegExp = new RegExp(/\d+/, 'g')
      elements.forEach(time => {
        const [hours, minutes] = time.match(numbersRegExp)
        hoursSum += Number(hours)
        minutesSum += Number(minutes)
      })
      const hoursFromMinutes = Math.floor(minutesSum / 60)
      const restMinutes = minutesSum % 60
      hoursSum += hoursFromMinutes
      minutesSum = restMinutes
      return {
          hours: hoursSum,
          minutes: minutesSum
      }
    }
    howManyDaysOfLearing(time) {
        const minutesInHour = 60
        const minutesSum = time.hours * minutesInHour + time.minutes
        return minutesSum / (minutesInHour * this.learningHoursPerDay)
    }
    display() {
        console.log(`Assuming you'll be learing ${this.learningHoursPerDay} hours per days, it will take %d days`, this.howManyDaysOfLearing(this.sumCoursesTime()))
    }
}
const OneHoursPerDay = new LearingHoursCounter({learningHoursPerDay: 1})
const TwoHoursPerDay = new LearingHoursCounter({learningHoursPerDay: 2})
const ThreeHoursPerDay = new LearingHoursCounter({learningHoursPerDay: 3})
const FourHoursPerDay = new LearingHoursCounter({learningHoursPerDay: 4})
OneHoursPerDay.display()
TwoHoursPerDay.display()
ThreeHoursPerDay.display()
FourHoursPerDay.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment