Skip to content

Instantly share code, notes, and snippets.

@siddrc
Created February 14, 2018 15:09
Show Gist options
  • Save siddrc/880759274face7b31ce5f7df2a7cd2b9 to your computer and use it in GitHub Desktop.
Save siddrc/880759274face7b31ce5f7df2a7cd2b9 to your computer and use it in GitHub Desktop.
time chaing class code puzzle solution
const timeData = [{
"name" : "lect1",
"time": "2:20"
}, {
"name" : "lect2",
"time": "3:30"
}, {
"name" : "lect1",
"time": "4:40"
}, {
"name" : "lect3",
"time": "3:30"
}];
//select all objects having lect1
const filtered = timeData.filter(item => item.name.includes("lect1") )
//and make an array of only their time strings
.map(item=>item.time)
//convert the time string into array of added seconds
.map(timeData => {
console.log(timeData)
const parts = timeData.split(":").map(part => parseFloat(part))
return ( parts[0]*60 ) + parts[1]
})
//finally use reduce to display total time of lect 1.
.reduce((prev,current)=> prev+current ,0)
console.log(filtered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment