1. /**
  2. * Flags anomalies in an array based on hard-coded limits.
  3. *
  4. * @param {Array<number>} arr The input array of numbers.
  5. * @param {object} limits An object containing the limits for anomaly detection.
  6. * Example: { min: 0, max: 100 }
  7. * @param {function} anomalyThreshold A function that determines if a value is an anomaly.
  8. * Defaults to checking if a value is outside the defined limits.
  9. * @returns {Array<number>} An array of boolean values, where true indicates an anomaly.
  10. */
  11. function flagArrayAnomalies(arr, limits, anomalyThreshold) {
  12. if (!Array.isArray(arr)) {
  13. console.warn("Input is not an array.");
  14. return [];
  15. }
  16. if (typeof limits !== 'object' || limits === null) {
  17. console.warn("Limits must be an object.");
  18. return new Array(arr.length).fill(false);
  19. }
  20. if (typeof anomalyThreshold !== 'function') {
  21. anomalyThreshold = (value) => {
  22. // Default anomaly check: outside min/max limits
  23. return (value < limits.min || value > limits.max);
  24. };
  25. }
  26. const anomalyFlags = [];
  27. for (let i = 0; i < arr.length; i++) {
  28. const value = arr[i];
  29. if (anomalyThreshold(value)) {
  30. anomalyFlags.push(true);
  31. } else {
  32. anomalyFlags.push(false);
  33. }
  34. }
  35. return anomalyFlags;
  36. }

Add your comment