Calculate the average distance between two data sets of X and Y (in JS)

๐Ÿ‘:-2

You could use map() to calculat the differences, reduce() to get the total and then divide by the length.

const distancesA = [17, 54, 87, 101, 120];
const distancesB = [12, 57, 82, 100, 125];

const averageDistance = distancesA.map((d, i) => d - distancesB[i]).reduce((acc, val) => acc + val, 0) / distancesA.length;

console.log(averageDistance)

Leave a comment