Created
February 14, 2018 15:09
-
-
Save siddrc/880759274face7b31ce5f7df2a7cd2b9 to your computer and use it in GitHub Desktop.
time chaing class code puzzle solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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