-1

Using Express 4 as my server engine. When I run the following function, the window becomes unresponsive. No errors in console.

function updateLoadedData(type, schedule, date) {
  var dateYear = date.year()
  var dateMonth = date.month()
  var dateDate = date.date()

  if (dateData[dateYear] == undefined)
    dateData[dateYear] = new Array()
  if (dateData[dateYear][dateMonth] == undefined)
    dateData[dateYear][dateMonth] = new Array()
  if (dateData[dateYear][dateMonth][dateDate] == undefined)
    dateData[dateYear][dateMonth][dateDate] = new Array()

  var output = ["hello", "stackoverflow"]

  dateData[dateYear][dateMonth][dateDate] = output
}

I'm at a loss trying to figure out what is causing this bug. Any help appreciated. Thanks in advance.

Jack Robson
  • 2,184
  • 4
  • 27
  • 50
  • Not sure, but don't you have to return something from this function? – Shilly Mar 02 '17 at 13:27
  • Between [**debugger**](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/debugger), [**console.log**](https://developer.mozilla.org/en/docs/Web/API/Console/log) statements and debugging using the browser console (F12 in Chrome) applying breakpoints in the scripts you should be able to pinpoint what's going on. – Nope Mar 02 '17 at 13:28
  • I've simplified the error causing code. It's something todo with with the 3D array. – Jack Robson Mar 02 '17 at 13:29
  • So what is `dateDate` when you do a console.log() on it? – rasmeister Mar 02 '17 at 13:46

2 Answers2

1

The problem must be somewhere else. Consider this implementation of your code. No errors.

You do have alot of ommitted {} and ; so if you're in strict mode, that might also be an issue.

var dateData = {};
var update = function updateLoadedData(type, schedule, date) {
  var dateYear = date.year()
  var dateMonth = date.month()
  var dateDate = date.date()

  if (dateData[dateYear] == undefined)
    dateData[dateYear] = new Array()
  if (dateData[dateYear][dateMonth] == undefined)
    dateData[dateYear][dateMonth] = new Array()
  if (dateData[dateYear][dateMonth][dateDate] == undefined)
    dateData[dateYear][dateMonth][dateDate] = new Array()

  var output = ["hello", "stackoverflow"]

  dateData[dateYear][dateMonth][dateDate] = output
};
update(null,null, {
 'year' : function(){ return 2017;},
 'month' : function(){ return 3;},
 'date' : function(){ return 2;},
});
alert(JSON.stringify(dateData));

Also note, that if dateData is also an array and not an object, the first 2016 keys of that array will be null. So if you loop over this, you might run into long running script issues if your loop doesn't handle null entries.

This is why you should .push() to arrays instead of setting exact indexes, who will cause 'holes' in the array. So I have a hunch you're better off using nested objects instead of 3d arrays.

Shilly
  • 8,511
  • 1
  • 18
  • 24
0

Looks to me like it is tied to your last statement:

dateData[dateYear][dateMonth][dateDate] = output

You are extending a 2-D array to 3-D.

The way I would debug this is to comment out that line and see if the window stays responsive. That will tell you if that line is the culprit. If it is, then you can add a console.log to see what your dateDate value is. Perhaps it is bogus.

Also, you might want to study up on multi-dimensional arrays. This link is a good place to start.

And as others have pointed out, consider a different way to represent your data. Multi-dimensional arrays (esp above 2) are usually represented in a different form.

Community
  • 1
  • 1
rasmeister
  • 1,986
  • 1
  • 13
  • 19