Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
How to get the average data with array function?

I'm a newbie for javascript. I have two variable, one is Ax2 and the other is Ay2, they will be constantly updated, I want to get the average of their latest refreshed 10 profiles, so I tried this, but it didn't work, can anyone help me?

var Ax2 = 0
var Ay2 = 0 
connection.on("ReceiveMessage", function (target) {
    Ax2 = `${target} `
   document.getElementById('Ax2').textContent = Ax2 ;
});
connection.on("ReceiveMessage2", function (target2) {   
    Ay2 = `${target2} ` * -1
    document.getElementById('Ay2').textContent = Ay2 ;
});

let arr1 = new Array(10);
let arr2 = new Array(10);

function ArrayAvg1(arr1) {
    var i = -1, summ = 0, ArrayLen1 = arr1.length;
    while (i < ArrayLen1) {
        arr1.push(Ax2)
        summ = summ + arr1[i++];
    }
    return summ / ArrayLen1;   
}
function ArrayAvg2(arr2) {
    var i = -1, summ = 0, ArrayLen2 = arr2.length;
    while (i < ArrayLen2) {
        arr2.push(Ay2)
        summ = summ + arr2[i++];
    }
    return summ / ArrayLen2;
}
var AX_after_correction = Ax2 - ArrayAvg1(arr1)
var AY_after_correction = Ay2 - ArrayAvg2(arr2)
var AX_after_correction = document.getElementById('AX_after_correction')
var AY_after_correction = document.getElementById('AY_after_correction')
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this code below:

var Ax2 = 0;
var Ay2 = 0;
let arrR = new Array(10);

connection.on("ReceiveMessage", function (;target) {
    Ax2 = `${target} `
   document.getElementById('Ax2').textContent = Ax2 ;
});
connection.on("ReceiveMessage2", function (target2) {   
    Ay2 = `${target2} ` * -1
    document.getElementById('Ay2').textContent = Ay2 ;
});

function ArrayAvg1(arr, val) {
  arr.push(val)
  let count = 0
  let numOr0 = n => isNaN(n) ? 0 : n
  arr.forEach(num => (typeof num) && count++)
  let sum = arr.reduce((a, b) => numOr0(a) + numOr0(b))
    return sum / count;   
}


var AX_after_correction = Ax2 - ArrayAvg1(arrR, Ax2)
var AY_after_correction = Ay2 - ArrayAvg1(arrR, Ay2)
var AX_after_correction = document.getElementById('AX_after_correction')
var AY_after_correction = document.getElementById('AY_after_correction')

But i think arrR will be return new Array(10) each times re-run code.

Which framework you use to build app?

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not sure what you're trying to achieve but:

  • you should pay attention to your variables naming as it gets hard to read and thus, to understand
  • try not to clutter your example with unnecessary information: you have ArrayAvg1 and 2 doing the same thing, the 'connection' part is not needed either etc...
  • you should avoid using var and go for either let or const,

That being said, assuming you have an array of numbers, there are a few ways to get the average value.

AFAIC, I would go for this one:

const average = (array) => array.reduce((a, b) => a + b) / array.length;

The array.reduce() function can be hard to understand at first... Here, it's walking the array and summing each current value binto the accumulator a, and returns the sum.

const average = (array) => ...;

This notation lets you declare an arrow function that takes an array as a parameter.

array.reduce((a, b) => a + b) / array.length is basically returning the sum of the values of the array divided by the array's length.

As for what you're doing

var Ax2 = 0 // updated on an event

let arr1 = new Array(10);

function ArrayAvg1(arr1) {
    var i = -1, summ = 0, ArrayLen1 = arr1.length; 
    while (i < ArrayLen1) {
        arr1.push(Ax2) // you're pushing Ax2 10 times ? 
        summ = summ + arr1[i++]; // this is weird
    }
    return summ / ArrayLen1;   
}

As I don't really understand what's your goal, I can't help you further.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda