• I am trying to run NRF.setRSSIHandler 10 times and print the average value. After that wait for 5 second and rerun the loop again for infinite time. But for JavaScript's callback function issue, it is not giving the expected output. I've tried using several closure solutions but none of them work. Is there any way to get the correct output?

    Code:

    let count = 0;
    let average = 0;
    //infinite loop
    while (count <1){
      for (let i = 0; i < 10; i++) {
        NRF.setRSSIHandler( function (rssi) {
          let distance = Math.pow(10, ((-56 - (rssi)) / (20))) * 3.28084;
          average = average + distance;
          NRF.setRSSIHandler();
        });
      }
      console.log(average/i);
      wait(5000);
    }
    
About

Avatar for user148837 @user148837 started