Skip to content

Instantly share code, notes, and snippets.

@cfinn16
Created April 12, 2019 17:53
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 cfinn16/80ef146a20aeff1d22b866d2132ccde8 to your computer and use it in GitHub Desktop.
Save cfinn16/80ef146a20aeff1d22b866d2132ccde8 to your computer and use it in GitHub Desktop.
Updated Solution to Third Problem
const bingeWatching = (timeElapsed, duration, action, amount) => {
const timeStrToSeconds = (str) => {
newArr = str.split(':').map(Number)
return (newArr[0] * 3600) + (newArr[1] * 60) + newArr[2]
}
const secondsToTimeStr = (seconds) => {
let hours = Math.floor(seconds / 3600)
if (hours === 0) {
hours = "00"
}
let minutes = (seconds % 3600) / 60
if (minutes === 0) {
minutes = "00"
}
let remainingSeconds = seconds - (hours * 3600) - (minutes * 60)
if (remainingSeconds === 0) {
remainingSeconds = "00"
}
return "" + hours + ":" + minutes + ":" + remainingSeconds
}
if (action === "Report Progress") {
return timeStrToSeconds(timeElapsed) / timeStrToSeconds(duration) * 100
} else if (action === "Rewind") {
let newTime
if (typeof amount === "string") {
newTime = timeStrToSeconds(timeElapsed) - timeStrToSeconds(amount)
} else if (typeof amount === "number") {
newTime = timeStrToSeconds(timeElapsed) * (amount / 100)
}
return newTime < 0 ? "00:00:00" : secondsToTimeStr(newTime)
} else if (action === "Fast Forward") {
let newTime
if (typeof amount === "string") {
newTime = timeStrToSeconds(timeElapsed) + timeStrToSeconds(amount)
} else if (typeof amount === "number") {
newTime = timeStrToSeconds(timeElapsed) + (timeStrToSeconds(duration) - timeStrToSeconds(timeElapsed)) * (amount / 100)
}
return newTime > timeStrToSeconds(duration) ? "Movie Finished" : secondsToTimeStr(newTime)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment