sum of uint8array javascript - javascript

I'm trying to sum and then average a stream of data, some code here.
var getAverage = function(dataArray){
var total,
sample = dataArray.length,
eArray = Array.prototype.slice.call(dataArray);
for (var i = 0; i< sample; i++) {
total+= eArray[i];
}
return total;
}
var output = function(){
//source data
var dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);
var average = getAverage(dataArray);
$('#average').text(average);
window.requestAnimationFrame(output);
Every element in the array returns a number, but it still returns NaN. Help?

Set total = 0; currently it is defaulting to undefined. undefined + a number = NaN, and NaN + a number = NaN.

The declared variable total is undefined which means it will create NaN (Not-a-Number) when a number is added to it.
Also, Typed Array (ArrayBuffer/views) and Array are not the same, and converting a typed array to an ordinary Array is making iteration slower as typed arrays are actual byte-buffers while Arrays are (node) lists. That on top of the cost of conversion itself.
Just add them straight forward. Remember to divide the sum on length and of course to initialize total:
var getAverage = function(dataArray){
var total = 0, // initialize to 0
i = 0, length = dataArray.length;
while(i < length) total += dataArray[i++]; // add all
return length ? total / length : 0; // divide (when length !== 0)
}

Related

For loop returning completely wrong value using power function

As you can see int he code below I separate the n variable and convert back to an integer array. When I put these numbers into my for loop they come out in the billions. Any help would be greatly appreciated.
var n = [86]
var p = [1]
n = n.toString().split("").map(function(value) {
return(parseInt(value))
})
var total = 0;
for(i=0;i<n.length;i++) {
total += (Math.pow(n[i],(p+i))
}
console.log(total)
You need the conversion from strings to numbers and you are adding an array to numbers.
You cannot sum an array to an integer [1]+0, it doesn't result in a number. It will do a concatenation.
var n = [86]
var p = [1]
n = n.toString().split("").map(val => Number(val))
var total = 0;
for (i = 0; i < n.length; i++) {
total += Math.pow(n[i], (p[0] + i))
}
console.log(total);
p is an array not an individual number, in your for loop, when it is running (p+i), that value ends up being 10 and 11. So your first loop is running Math.pow(8,10) and the second time it runs it is running Math.pow(6,11), which, will result in a number in the billions

How to split String, convert to Numbers and Sum

I have a function that I have modified to get a string (which consists of zeros and ones only).
The string (timesheetcoldata):
100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000
The string items (the numbers one and zero) will change every time the function is run.
It will always be the same length.
I have made the string above easier to see what I am trying to achieve.
I want to return the first character and then every 24th character (as in the variable colsCount in the function).
so, in the example above, it would return something like: 111111
I then want to convert these characters to numbers (something like [1, 1, 1, 1, 1, 1]).
I then want to sum these number together (so it would return, in the example: 6).
I then want to check if the returned number matches the variable: rowsCount
or true if it does, false if it does not.
My function:
$("#J_timingSubmit").click(function(ev){
var sheetStates = sheet.getSheetStates();
var rowsCount = 6;
var colsCount = 24;
var timesheetrowsdata = "";
var timesheetcoldata = "";
for(var row= 0, rowStates=[]; row<rowsCount; ++row){
rowStates = sheetStates[row];
timesheetrowsdata += rowStates+(row==rowsCount-1?'':',');
}
timesheetcoldata = timesheetrowsdata.replace(/,/g, '');
console.log(timesheetcoldata);
});
Thank you very much to both Rajesh and MauriceNino (and all other contributers).
With their code I was able to come up with the following working function:
$("#J_timingSubmit").click(function(ev){
var sheetStates = sheet.getSheetStates();
var rowsCount = 6;
var timesheetrowsdata = "";
var timesheetcoldata = "";
for(var row= 0, rowStates=[]; row<rowsCount; ++row){
rowStates = sheetStates[row];
timesheetrowsdata += rowStates+(row==rowsCount-1?'':',');
}
timesheetcoldata = timesheetrowsdata.replace(/,/g, '');
var count = 0;
var list = [];
for(var i = 0; i< timesheetcoldata.length; i+=24) {
const num1 = Number(timesheetcoldata.charAt(i));
list.push(num1);
count += num1;
}
let isSameAsRowsCount = count == rowsCount;
console.log('Is Same? ', isSameAsRowsCount);
});
You can always rely on traditional for for such action. Using functional operations can be more readable but will be more time consuming(though not by much).
You can try this simple algo:
Create a list that will hold all numbers and a count variable to hold sum.
Loop over string. As string is fixed, you can set the increment factor to the count(24).
Convert the character at given index and save it in a variable.
Push this variable in list and also compute sum at every interval.
At the end of this loop, you have both values.
var string = '100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000';
var count = 0;
var list = [];
for(var i = 0; i< string.length; i+=24) {
const num1 = Number(string.charAt(i));
list.push(num1);
count += num1;
}
console.log(list, count)
Here is a step by step explanation, on what to do.
Use match() to get every nth char
Use map() to convert your array elements
Use reduce() to sum your array elements
Everything needed to say is included in code comments:
const testData = '100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000';
// Step 1) Create array of numbers from string
const dataArr = testData.match(/.{1,24}/g) // Split on every 24th char
.map(s => Number(s[0])) // Only take the first char as a Number
console.log(dataArr);
// Step 2) Sum array Numbers
let dataSum = dataArr.reduce((a, b) => a + b); // Add up all numbers
console.log(dataSum);
// Step 3) Compare your variables
let rowsCount = 123; // Your Test variable
let isSameAsRowsCount = dataSum == rowsCount;
console.log('Is Same? ', isSameAsRowsCount);
As #Jaromanda mentioned, you can use the following to done this.
const string = '100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000';
const value = string.split('').filter((e,i)=> !(i%24)).reduce((acc,cur)=> acc+ (+cur), 0);
console.log(value);

for loop to calculate average doesn't work as intended

I have a coding challenge in which I need to create some sort of an average calculator. As it's a beginner's course, the challenge is quite straightforward ( just input a bunch of variables and add them ). However I tried to use a for loop to make my life easier.
It keeps giving me the "NaN" answer when I console log it.
I don't really know what's wrong here. It seems relatively logical from my noob perspective. I tried putting the average var inside the for loop, but it would just average the 1st and 2nd, then move onto 2nd and 3rd, and then finally give me NaN again.
var johnTeam, mikeTeam;
var johnAverage,mikeAverage;
johnTeam = [89,120,103];
mikeTeam = [116,94,123];
function averageCalc(){
var i;
for (i in johnTeam){
var j=i++;
}
var average=(johnTeam[i]+johnTeam[j])/3;
console.log(average)
}
Expected result should be '104'.
Current result 'NaN'.
You'd never use for in to loop an array, you usually don't use i after the loop. Also, i in your case is always only the last counter value. To get the sum of an array, you'd usually use reduce and to get the average you'll divide earlier result by length
const johnTeam = [89,120,103];
const mikeTeam = [116,94,123];
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const johnAvg = average(johnTeam);
const mikeAvg = average(mikeTeam);
console.log(johnAvg);
console.log(mikeAvg);
Also consider using this :
var sumjohnTeam = 0;
johnTeam.forEach(p => (sumjohnTeam += p));
var avgjohnTeam= sumjohnTeam /johnTeam.length;
The arithmetic mean is computed by taking the sum of all the measurements and dividing by the total number of measurements. If you want to be a good statistician, null/undefined measurement counts toward N, the number of measurements, but not towards the sum of all measurements. That gives us this:
function mean( values ) {
if (!values || !value.length) return undefined;
let sum = 0;
for ( let i = 0 ; i < values.length ; ++i ) {
const value = values[i];
if (value == undefined || value == null) continue;
sum += value ;
}
return sum / values.length ;
}
With that, it's easy:
const johnTeam = [89,120,103];
const johnAverage = mean( johnTeam ) ;
const mikeTeam = [116,94,123];
const mikeAverage = mean( mikeTeak ) ;
You just need to calculate the sum in the loop then outside you divide by the number of elements in you array.
Here's an updated version of your try :
var johnTeam = [89, 120, 103],
mikeTeam = [116, 94, 123],
johnAverage = 0,
mikeAverage = 0;
// added a parameter so you can send the team array you'll want to calculate its average.
function averageCalc(teamArray) {
// stores the sum.
var sum = 0;
// loop and calculate the sum.
// notice the teamArray[i] not only i.
for (var i in teamArray) sum += teamArray[i];
// calculate average based on the array length and logs it out.
console.log(sum / teamArray.length);
}
// tests
averageCalc(johnTeam); // 104
averageCalc(mikeTeam); // 111

Separating a string into integers and storing sum and averages

JavaScript; I want to get the values in a string which always has the following format but also has an indeterminate number of variables:
Sample String "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4"
The variables named as 340, 339, 338 etc. in this string store integer values such as 5, 4, 1 and so on. I want to extract the integer values for each of the variables (extracting what's after the equal signs) and store the sum and average value of them in two separate variables as "sum" and "avr".
Nothing fancy, hopefully nice and readable.
function calculate(myString) {
var values = myString.split(';');
var sum = 0;
values.forEach(function(el, i) {
sum += parseInt(el.split('=')[1]);
});
return {
sum: sum,
avg: sum / values.length
}
}
var results = calculate("340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4");
console.log(results.sum); // => 30
console.log(results.avg); // => 3.75
As you have a simple pattern, you can use a RegExp to find the bits of interest. Here is an example using String.prototype.split and Array.prototype.reduce
var str = "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var parts = (';'+str).split(/;\d+=/).slice(1), // passing the RegExp to .split
sum = parts.reduce(function(prev, cur) {
return prev + +cur; // notice String cur to Number
}, 0),
avr = sum / parts.length; // assuming you want mean average
console.log(sum, avr); // 30 3.75
I would split the string then loop through all the values to get the sum and average.
var str="340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var array =str.split(/;?\d+=/g);
var sum=0;
for(i=0;i<array.length;i++){
if(!isNaN(parseInt(array[i])))
sum+=parseInt(array[i]);
else{
array.splice(i,1);
i--;
}
}
console.log(sum);
console.log(sum/array.length);
This is trivial string evaluation. The only method you need is split, and the only logic you need is a for loop.
var values = "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var sum = []
var avr = []
values = values.split(';');
for(i=0; i<values.length; i++){
numbers = values[i].split('=')
sum.push(numbers[0])
avr.push(numbers[1])
}

Treat the prev element in JavaScript array when creating object dynamically

I want to do something like:
var arr = []
for var(i=0;i<x;i++){
arr.push{ get num(){return this.previousArrayElement.num + randomNumber}}
}
how can I treat "previousArrayElement"?
I think you are just trying to create an array of size x containing numbers in order of size and separated by randomNumber intervals? Something like this would work:
var x = 100;
var arr = [0]
for (i=1; i<x; i++) {
arr.push( arr[i-1] + Math.random() );
}
Note that by starting the array out with an initial value (index 0) and beginning your iteration with the second value (index 1) you don't have to worry about accessing the 0-1 element at the first iteration.
I hope that helps!
Not 100% sure this is what you want. Expected output shown is not valid syntax and details provided are very open to interpretation
var arr = []
for (var i=0; i < x; i++){
var num = i > 0 ? arr[i-1].num : 0;
num= num + randomNumber; // is this an existing variable?
arr.push({ num: num}); // used object with property `num` based on example `previousArrayElement.num `
}

Categories