Nested iteration using a for loop in Javascript

I am attempting to iterate through a large array of values and calculate the average of one of the values for each second. I seem to be having trouble getting this code to function correctly, and it appears that the issue lies within the nested while loop. Could it be possible that I made a scope error preventing me from iterating the for loop index?

The data contains timestamps in milliseconds and radiation counts. a.data[i][0] represents the timestamp and a.data[i][26] represents the count.

for (i = 0; i < a.data.length; i++){
    // counts is the count of radiation over the last timeframe
    var counts = 0;
    // t1 is the start time 
    // t2 is the current iteration time
    var t1, t2 = a.data[i][0];
    while ((t2 - t1) < 1000){
        t2 = a.data[i][0];
        counts += a.data[i][26];
        i++;
    }
    // Geiger Data is an array of { x:(time), y:(value)} datapoints.
    GeigerData.push({x: (t1/1000), y: counts});
}

Answer №1

The issue you are facing originates from this specific line of code:

 var t1, t2 = a.data[i][0];

It seems like there is a misunderstanding in how JavaScript variables should be defined. In your current implementation, t1 remains undefined. What you actually need to do is:

 var t1 = a.data[i][0];
 var t2 = t1;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

trigger a border hover effect on an HTML element

Is it possible to attach a mouseover event specifically to the left border of a div element in HTML? The div serves as a container for various other intricate HTML elements, each with their own mouseover events. While binding a mouseover event to the enti ...

incapable of destructuring two objects simultaneously

Is there a way to map movies using columns as a property reference? For example: {movies.map(item => {columns.map(column => item.column.path)})} When I try this, the result is always undefined. The 'movies' array contains detailed inform ...

Angular - Utilizing controllers without any associated HTML elements and utilizing broadcasted events

My situation is a bit peculiar. I have a controller that I need to manage when a dialog opens. I am utilizing ngDialog along with templates and directives. Below is the code for DialogController: (function() { 'use strict'; angular.modu ...

Implement Babel with node.js 6.9

It puzzles me why some folks opt to use Babel for their Node.js projects. I personally utilize node 6.9 and have no issues writing ES6 code - from default arguments in functions to arrow functions, rest parameters, and spread syntax. Do you think Babel is ...

Transform a string into an object

How can I convert a string into an object with error details? const er= {'username': [ErrorDetail(string='A user with that username already exists.', code='unique')], 'email': [ErrorDetail(string='user with thi ...

Is it advisable to send a response in Express.js or not?

When working with Express.js 4.x, I'm unsure whether to return the response (or next function) or not. So, which is preferred: Option A: app.get('/url', (req, res) => { res.send(200, { message: 'ok' }); }); Or Option B: ...

Sending various Python variables to PHP

CSS Below is a simple form where users can input a URL: <form method="post" action="/" accept-charset="utf-8"> <input type="text" name="url" placeholder="Enter a URL" /> <button>Submit</button> </form> PHP (CodeIgn ...

"Successfully implementing AJAX POST functionality, but encountering issues where callbacks are not triggering

I have gone through numerous posts addressing this issue and attempted various solutions, however, none seem to work for me. My AJAX POST function correctly adds the email to my mailing list, but the success and error callbacks are not consistently firing, ...

How can I effectively incorporate a new option into a dropdown menu created using the msDropdown plugin?

I am currently utilizing the jQuery plugin known as msDropdown to showcase dropdown lists that include images. I have been attempting to dynamically insert a new option and then update a msDropdown dropdown list, with the following code: dropdown.add(jso ...

Tips for transferring information from a controller to a directive using the $emit method

I am facing an issue passing values from the controller to the directive Within my controller, I have two arrays defined as: $scope.displayPeople.push(data.userName); $scope.displayOrg.push(data.orgname); I need to pass these arrays from the controller ...

Tips for maintaining a single-line div while adding ellipses:

What is the CSS way to ensure that a div or class contains only one line of text, with ellipses added if it exceeds that limit? I am aware that setting a specific height and using overflow:hidden can achieve this, but it doesn't quite work for my need ...

Setting up a new package through JPSM installation

I utilize jspm in my web application. My goal is to install the npm:angular2 package without needing to set it up in the config.js file. Instead of loading the angular2 module via jspm, I manually added the angular2 library like this: <script src="jspm ...

Developing a timeline using a JSON file and dynamically altering the page based on the specified datetime within the JSON data

I am currently working on a project that involves a JSON file containing information about missions. This JSON file is subject to continuous updates and has a specific structure as shown below: [ { "module_id": 5, "title": "TITLE 1", "media ...

Error: The script is unable to access the property "div" because it is undefined

I keep encountering this issue "Cannot read property 'div' of undefined" This is the snippet in question function validateData(){ for(let j = 0; j < 13; j++){ if(dataArr[j].data.textContent === resultSet[j].result.div. ...

Ways to customize decoration in Material UI TextField

Struggling to adjust the default 14px padding-left applied by startAdornment in order to make the adornment occupy half of the TextField. Having trouble styling the startAdornment overall. Attempts to style the div itself have been partially successful, b ...

Guide to setting up parameterized routes in GatsbyJS

I am looking to implement a route in my Gatsby-generated website that uses a slug as a parameter. Specifically, I have a collection of projects located at the route /projects/<slug>. Typically, when using React Router, I would define a route like t ...

Having difficulty maintaining trailing zeroes in decimals after converting to float in Angular

I need assistance with converting a string to float in Angular. Whenever I use parseFloat, it seems to remove the zeros from the decimal values. How can I ensure that these zeros are retained with the numerical values? The example below should provide more ...

Using an array as a constant within a PHP class

Similar Inquiry: Is it viable to define an array as constant? Can an array be utilized as a class constant within PHP? For example: const MYARRAY = array('123', '234'); If not, what is the reason for this limitation? ...

The functionality of fetching website titles using Ajax/jQuery is experiencing some limitations

Below is a code snippet for a simple website title getter: $( document ).ready(function() { $("#title").click(function() { var SubURL = $("#input").val(); $.ajax({ url: "http://textance.herokuapp.com/title/"+SubURL+"/", complete: function(da ...

Unable to locate module: '@material-ui/pickers' - Material UI React

I encountered an error that said: Material UI React - Module not found: Can't resolve '@material-ui/pickers' in React. Previously, I faced a similar issue with '@date-io/date-fns' but was able to fix it by updating to the latest ...