calculates the mean response times for each person in javascript

To calculate the mean response time for each person, you can follow these steps:

  1. Collect response times for each person in an array of objects. Each object should have two properties: the person's name and an array of response times.
index.tsx
const responseTimes = [
  { name: 'Alice', times: [10, 12, 14, 8, 9] },
  { name: 'Bob', times: [16, 18, 20, 22, 19] },
  { name: 'Charlie', times: [7, 9, 12, 15, 13] }
];
172 chars
6 lines
  1. Use the map method to iterate over the array of response times and calculate the mean response time for each person.
index.tsx
const meanResponseTimes = responseTimes.map(person => {
  const totalResponseTime = person.times.reduce((sum, time) => sum + time, 0);
  const meanResponseTime = totalResponseTime / person.times.length;
  return { name: person.name, mean: meanResponseTime };
});
263 chars
6 lines
  1. The resulting array will have an object for each person with their name and mean response time.
index.tsx
console.log(meanResponseTimes);
// Output:
// [
//   { name: 'Alice', mean: 10.6 },
//   { name: 'Bob', mean: 19 },
//   { name: 'Charlie', mean: 11.2 }
// ]
158 chars
8 lines

gistlibby LogSnag