average in javascript

To find the average of a set of numbers in JavaScript, you can use the reduce method on an array.

Here's an example:

index.tsx
const numbers = [5, 10, 15, 20, 25];

const average = numbers.reduce((total, num) => {
  return total + num;
}) / numbers.length;

console.log(average); // Output: 15
167 chars
8 lines

In this example, we start with an array of numbers. We use the reduce function to sum all the numbers in the array. After that, we divide by the number of elements in the array to get the average value. Output will print 15 which is the average of 5, 10, 15, 20, 25.

gistlibby LogSnag