Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda