Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2038

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T01:44:09+00:00 2024-11-26T01:44:09+00:00

JavaScript-30-Day-18

  • 61k

Adding up times with Reduce

demo

ss

Today we will be using Array.prototype.map and Array.prototype.reduce to figure out the total runtime of given videos in hours, minutes and seconds.

So we are given a list of items each mimicking a video and each one has a data-time attribute telling us the length of the video in the format minutes:seconds, and we need to figure out how to pull them out of the DOM, convert them to numbers, make them into minutes and seconds, add it all up, figure out how many hours, minutes and seconds there are totally, then finally console.log those actual values.

Here is sample of list items given:

<ul class="videos">       <li data-time="5:43">Video 1</li>       <li data-time="2:33">Video 2</li>       <li data-time="3:45">Video 3</li>       <li data-time="0:47">Video 4</li>       <li data-time="5:21">Video 5</li> </ul> 
Enter fullscreen mode Exit fullscreen mode

First thing we want to do is select all of those nodes

const timeNodes = document.querySelectorAll("[data-time]"); 
Enter fullscreen mode Exit fullscreen mode

We are not selecting list items because we don't know if it's always going to be a list item so we will select anything with a data-time attribute on it.

Now this timeNodes is a NodeList and to use map we need an Array, so we need to convert this from a node list into an array of actual time strings.

const timeNodes = Array.from(document.querySelectorAll("[data-time]")); 
Enter fullscreen mode Exit fullscreen mode

Now timeNodes is an array of list items.

We will now extract time out of this array of list items.

const seconds = timeNodes.map((node) => node.dataset.time); 
Enter fullscreen mode Exit fullscreen mode

Now seconds is an array of strings (containing all the times)

Now we want to convert the time into just seconds. The format in which we have our time right now is minutes:seconds so we need to split this string on the :.

const seconds = timeNodes.map((node) => node.dataset.time).map((timeCode) => {           const [mins, secs] = timeCode.split(":"); 
Enter fullscreen mode Exit fullscreen mode

But the problem we run into here is after splitting what we get is also a string and we need numbers to do any kind of mathematical calculation.

1

So we need to map over the array again and use parseFloat to convert the string into numbers.

We can see how parseFloat solves our problem:

2

const seconds = timeNodes         .map((node) => node.dataset.time)         .map((timeCode) => {           const [mins, secs] = timeCode.split(":").map(parseFloat); return mins * 60 + secs }); 
Enter fullscreen mode Exit fullscreen mode

This gives us all the time in seconds.

3

Now that we have all the times in seconds, we need to add them all up and we are going to use reduce for that.

const seconds = timeNodes         .map((node) => node.dataset.time)         .map((timeCode) => {           const [mins, secs] = timeCode.split(":").map(parseFloat);           return mins * 60 + secs;         })         .reduce((total, vidSeconds) => total + vidSeconds); 
Enter fullscreen mode Exit fullscreen mode

This gives us the total number of seconds of every single video added together.

Time to chop down the total seconds into hours, minutes and seconds.

      let secondsLeft = seconds;       const hours = Math.floor(secondsLeft / 3600); 
Enter fullscreen mode Exit fullscreen mode

We use Math.floor to get rid of the decimal part, for example 2.5 hours needs to be converted to 2 hours and we'll convert the remaining 0.5 hours into minutes and seconds

1hour=3600seconds

      secondsLeft = secondsLeft % 3600;       const mins = Math.floor(secondsLeft / 60);       //1min=60seconds       secondsLeft = secondsLeft % 60; console.log(`${hours}hours ${mins}minutes ${secondsLeft}seconds`); 
Enter fullscreen mode Exit fullscreen mode

This is final output:

4

and with this our project for the day was completed.

GitHub repo:

GitHub logo cenacrharsh / JS-30-DAY-18

Blog on Day-17 of javascript30

cenacr007_harsh

JavaScript-30-Day-17

KUMAR HARSH ・ Jun 17 '21

#javascript #webdev #github #programming

Blog on Day-16 of javascript30

cenacr007_harsh

JavaScript-30-Day-16

KUMAR HARSH ・ Jun 16 '21

#javascript #css #github #webdev

Blog on Day-15 of javascript30

cenacr007_harsh

JavaScript-30-Day-15

KUMAR HARSH ・ Jun 15 '21

#javascript #github

Follow me on Twitter

Follow me on Linkedin

DEV Profile

You can also do the challenge at javascript30

Thanks @wesbos , WesBos to share this with us! 😊💖

Please comment and let me know your views

Thank You!

githubjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.