Search code examples
javascriptarrayscountthreshold

Which javascript function would return the number of values under a threshold in an array


I have a processAverage function that returns a value Z by averaging two values sent from a connected device. If this value Z is lower than a threshold (this.limitZ) it triggers an alert on a server.

However, the alert is sent too quickly in my opinion and I would like to slow it down with a function that sends the alert only if the value returned is lower than the threshold limitZ 5 times in the last 10 equations of processAverage.

This is what I came up with so far. First I keep the last 10 results of processAverage in a sliding array:

const delimiter = '\n'
const eValues = []
const elmt = []

this.limitZ = 45
const z = this.processAverage(e1, e3)
const i = 0;
do
{i += 1
elmt.push (z)
}
while (i < 10)

Now, I have to write a function that returns all the values in the array that are lower than the threshold this.limitZ.

for exemple

var filtered = items.filter(function(item) {
  return item < this.limitZ;
});
console.log(filtered);

count(filtered) {
  const =filtered;
  return 
}

And then I need a function that would emit the alert when the length of this function filtered is above 5 items. It would probably look like:

 if (filtered.length > 5) {
          this.emit('alert', msg)

However, I'm not sure how to properly write it and have these functions work together as I don't know JS and everything I try to run tells me there are issues with the variables but I'm also sure there is a simpler way to trigger the alert once 5 values in the array are under the threshold, right?

Thanks in advance


Solution

  • Your "sliding array" is not quite implemented correctly, as it will just stop pushing to the array once it has 10 copies of the first z value inside it.

    Here's a correct implementation. I've replaced your Z with just a random integer for demonstration purposes, and the for loop is only to show it populating and keeping only the last 10 numbers. In reality, you wouldn't use a loop and would instead push to the array every time you get a new Z value. (This could be done using an interval firing every N milliseconds, for example).

    function randomNumber(){ return Math.floor(Math.random() * 10);}
    
    var arr = [];
    let capacity = 10;
    for(let i = 0; i < 15; i++)
    {
      let z = randomNumber();
      if(arr.length == capacity)
        arr.splice(0, 1);
      arr.push(z);
    }
    
    console.log(arr);

    The second part is to get the number of elements in arr that are less than the threshold. I'll use 5 as the threshold for this. Don't let the lambda function scare you. It's basically the same thing as function(e){return e < threshold}

    let threshold = 5;
    let filtered = arr.filter(e => e <= threshold);
    //filtered.length contains the number of elements below the threshold
    

    function randomNumber(){ return Math.floor(Math.random() * 10);}
    
    var arr = [];
    let capacity = 10;
    for(let i = 0; i < 15; i++)
    {
      let z = randomNumber();
      if(arr.length == capacity)
        arr.splice(0, 1);
      arr.push(z);
    }
    
    console.log(arr);
    
    let threshold = 5;
    let filtered = arr.filter(e => e <= threshold);
    
    console.log(filtered);
    console.log(filtered.length);

    Then finally you could use filtered.length to emit when the number of values is greater than 5. You can run this snippet a few times until you see it both emit when the value is greater than 5 and not emit when the value is 5 or less.

    if(filtered.length > 5)
        this.emit(...);
    

    function randomNumber(){ return Math.floor(Math.random() * 10);}
    
    var arr = [];
    let capacity = 10;
    for(let i = 0; i < 15; i++)
    {
      let z = randomNumber();
      if(arr.length == capacity)
        arr.splice(0, 1);
      arr.push(z);
    }
    
    console.log(arr);
    
    let threshold = 5;
    let filtered = arr.filter(e => e <= threshold);
    
    console.log(filtered);
    console.log(filtered.length);
    
    if(filtered.length > 5)
      console.log("EMIT");