JSFiddle - React, Tailwind, and code Playground
by rsmclaug
HTML
Here you go, Ben. Hope this helps. Open this in Chrome and hit Cmd-Opt-I (Mac) or F12 (Windows) to open Chrome Dev Tools. The console statements will be logged there once you hit 'Run'.
JavaScript
// Example arrays
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var b = [1, 2, 3, 0, 0, 0, 0, 0];
var c = [1, 2, 0, 0, 0];
// Initialize all the variables
var i = 0, avg = [], dividend = 0, divisor = 0;
// While there is a value at any index (so it stops when we reach the end of the longest arrary),
// add each value to the dividend only if it exists and increment the divisor so we average by the
// correct number
do {
dividend = 0;
divisor = 0;
if (a[i] != undefined) {
dividend += a[i];
divisor++;
}
if (b[i] != undefined) {
dividend += b[i];
divisor++;
}
if (c[i] != undefined) {
dividend += c[i];
divisor++;
}
avg[i] = dividend / divisor;
console.log("The average of the " + divisor + " number(s) is: " + avg[i]);
i++;
} while (a[i] != undefined || b[i] != undefined || c[i] != undefined)
console.log("avg", avg);