我试图计算传感器返回的值的运行平均值,并在平均值满足两个条件(array.length == x && avg 此代码返回0。 当平均值为<阈值和array.length == length时,如何计算运行平均值并运行一些代码?let arr = []
let avg = 0
let sum = 0
sensor.calibrate(() => {
sensor.getMeasurement((volts) => {
console.log(volts) // typical values are ~0.9 V
runningAvg(volts, 0.85, 10)
})
})
function runningAvg(item, threshold, length) {
if (array.length < length) {
array.push(item)
} else {
array.shift().push(item)
for(let i=0; i < array.length; i++) {
sum += array[i]
}
avg = sum / length
}
if(avg < threshold && array.length == length) console.log(avg)
}