{"id":22345816,"url":"https://github.com/gerhynes/adding-times-with-reduce","last_synced_at":"2025-07-11T03:33:13.044Z","repository":{"id":106043825,"uuid":"107597523","full_name":"gerhynes/adding-times-with-reduce","owner":"gerhynes","description":"A script written to take string times from the DOM and add them. Part of Wes Bos' JavaScript 30 course.","archived":false,"fork":false,"pushed_at":"2018-01-25T21:28:25.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-26T10:23:58.447Z","etag":null,"topics":["javascript","javascript30","reduce"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gerhynes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-19T20:53:10.000Z","updated_at":"2023-04-11T15:21:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"c63303b3-28eb-4361-a6ba-741ab1b185ce","html_url":"https://github.com/gerhynes/adding-times-with-reduce","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gerhynes/adding-times-with-reduce","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fadding-times-with-reduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fadding-times-with-reduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fadding-times-with-reduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fadding-times-with-reduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gerhynes","download_url":"https://codeload.github.com/gerhynes/adding-times-with-reduce/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fadding-times-with-reduce/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264721524,"owners_count":23653948,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","javascript30","reduce"],"created_at":"2024-12-04T09:18:47.847Z","updated_at":"2025-07-11T03:33:12.782Z","avatar_url":"https://github.com/gerhynes.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Adding string times with reduce\n\nA script written to take string times from the DOM and add them. Part of Wes Bos' [JavaScript 30](https://javascript30.com/) course.\n\n![Screenshot of console showing total video times](https://res.cloudinary.com/gerhynes/image/upload/v1516830603/Screenshot_add_with_reduce_kzue2b.jpg)\n\n## Notes\n\nThe challenge is to take `data-time` attributes, which are strings, from the DOM, convert them to numbers, make them into minutes and seconds, and then add them together.\n\nFirst, select all of the time nodes, i.e. everything with a `data-time` attribute on it.\n\nThis will give you a NodeList of the lis. However, you can't use methods like `map` on this.\n\nUse `Array.from` to convert it to an array.\n\n```js\nconst timeNodes = Array.from(document.querySelectorAll(\"[data-time]\"));\n```\n\n`map` over the time nodes to get an array of all the times in strings.\n\n`map` again and `split` the array on `\":\"` to break it into minutes and seconds.\n\n(You can use ES6 destructuring here to get them into their own variables.)\n\nImmediately after you `split`, `map` it into `parseFloat` to turn the minutes and seconds from strings to numbers.\n\nNOTE: `map` takes in an array and exports an array. `reduce` takes in an array and can return whatever you need. Here you want to `reduce` everything down to one number.\n\n```js\nconst seconds = timeNodes\n  .map(node =\u003e node.dataset.time)\n  .map(timeCode =\u003e {\n    const [mins, secs] = timeCode.split(\":\").map(parseFloat);\n    // console.log(mins, secs);\n    return mins * 60 + secs;\n  })\n  .reduce((total, vidSeconds) =\u003e total + vidSeconds);\n```\n\nThis gives you the total number of seconds. Now you want to divide it into hours, minutes and seconds.\n\nCreate a let variable, `secondsLeft`.\n\nDivide `secondsLeft` by 3600, and use `Math.floor` to cut off everything after the decimal point, to get `hours`.\n\nRun `secondsLeft % 3600` to find out how many seconds are left over after you have the total number of hours.\n\nThen divide your updated `secondsLeft` by 60 and use `Math.floor` to cut off everything after the decimal point, to get `minutes`. Then use `secondsLeft % 60` to separate `seconds` and `minutes`.\n\nFinally, log all of them to the console.\n\n```js\nlet secondsLeft = seconds;\nconst hours = Math.floor(secondsLeft / 3600);\nsecondsLeft = secondsLeft % 3600;\n\nconst mins = Math.floor(secondsLeft / 60);\nsecondsLeft = secondsLeft % 60;\n\nconsole.log(hours, mins, secondsLeft);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Fadding-times-with-reduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgerhynes%2Fadding-times-with-reduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Fadding-times-with-reduce/lists"}