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')
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?
I'm not sure what you're trying to achieve but:
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.