/**
* Flags anomalies in an array based on hard-coded limits.
*
* @param {Array<number>} arr The input array of numbers.
* @param {object} limits An object containing the limits for anomaly detection.
* Example: { min: 0, max: 100 }
* @param {function} anomalyThreshold A function that determines if a value is an anomaly.
* Defaults to checking if a value is outside the defined limits.
* @returns {Array<number>} An array of boolean values, where true indicates an anomaly.
*/
function flagArrayAnomalies(arr, limits, anomalyThreshold) {
if (!Array.isArray(arr)) {
console.warn("Input is not an array.");
return [];
}
if (typeof limits !== 'object' || limits === null) {
console.warn("Limits must be an object.");
return new Array(arr.length).fill(false);
}
if (typeof anomalyThreshold !== 'function') {
anomalyThreshold = (value) => {
// Default anomaly check: outside min/max limits
return (value < limits.min || value > limits.max);
};
}
const anomalyFlags = [];
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
if (anomalyThreshold(value)) {
anomalyFlags.push(true);
} else {
anomalyFlags.push(false);
}
}
return anomalyFlags;
}
Add your comment