Languages
[Edit]
EN

TypeScript - average value calculation using array reduce()

3 points
Created by:
Imaan-Morin
1009

In this article, we would like to show you how to calculate the average value with reduce() method in TypeScript.

Runnable example:

const calculateAverage = (array: number[]): number => {
    const sum = array.reduce((a: number, b: number): number => a + b);
    return sum / array.length;
};


// Usage example:

const numbers: number[] = [10, 20, 30];
const average: number = calculateAverage(numbers);

console.log(average);  // 20

Output:

20

References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join