
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to calculate total time between a list of entries?
Let’s say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this −
Following is our sample array −
const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { direction: 'upstream', velocity: 40 }, { direction: 'upstream', velocity: 37.5 }]
We are required to write a function that takes in such type of array and finds the net velocity (i.e., velocity during upstream - velocity during downstream) of the boat during the whole course.
So, let’s write a function findNetVelocity(), iterate over the objects and calculate the net velocity. The full code for this function will be −
Example
const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { direction: 'upstream', velocity: 40 }, { direction: 'upstream', velocity: 37.5 }]; const findNetVelocity = (arr) => { const netVelocity = arr.reduce((acc, val) => { const { direction, velocity } = val; if(direction === 'upstream'){ return acc + velocity; }else{ return acc - velocity; }; }, 0); return netVelocity; }; console.log(findNetVelocity(arr));
Output
The output in the console will be −
67.5
- Related Articles
- Calculate total time duration (add time) in MySQL?
- PHP program to calculate the total time given an array of times
- How to calculate the accounting entries for forward contracts?
- How to calculate percentage of total in Excel?
- How to calculate time difference between two times or dates?
- How to calculate the difference between time in different MySQL columns?
- How to calculate running total /average in Excel?
- How to calculate the cumulative sum / running total of a column in Excel?
- C++ Program to Calculate Difference Between Two Time Period
- Golang Program to Calculate Difference Between Two Time Periods
- How to calculate Execution Time of a Code Snippet in C++?
- How to calculate rank percentile of a list in Excel?
- Program to remove duplicate entries in a list in Python
- C Program to calculate the difference between two time periods
- How to calculate local time in Node.js?

Advertisements