@nodescript/stdlib
Version:
Standard Node Definitions
33 lines (32 loc) • 676 B
JavaScript
export const module = {
version: '1.0.4',
moduleName: 'Math / Mean',
description: `
Computes the arithmetic mean of specified numbers.
`,
keywords: ['average'],
params: {
values: {
schema: {
type: 'array',
items: { type: 'number' },
},
},
},
result: {
schema: { type: 'number' },
}
};
export const compute = params => {
const { values } = params;
if (!values.length) {
return 0;
}
let sum = 0;
for (const num of values) {
if (!isNaN(num)) {
sum += num;
}
}
return sum / values.length;
};