2

I would like to have an array with timestamp as keys and numbers as values. This way I want to track how many cars have entered or left a parking lot as well as how many cars have parked in the parking lot simultaneously.

Basics: - Get the list of parking actions with enter date and exit date for each transaction - Get all those dates into an array with timestamp as key and += 1 if enter date and -=1 for exit date - Sort by date - Go through sorted array and add to a counter, track if new maximum is reached

    var result = [];
    var counter = 0;
    var max = 0;

    //SELECT enterTS, exitTS FROM parking;
    // validated, that works

    while (!rs.eof) {   
        var es = rs.fields(1).toString();
        var dd = rs.fields(2).toString();

        result[es] += 1;     //might happen at the same time with exit or other entries
        result[dd] -= 1;
        alert('Start' + es); //correct timestamp
        alert('Array' + result[es]); //shows NaN
    }

    result.sort();

    for (var key in result) {            
          counter += result[key];

          if(counter > max){
               max = counter;
          }
    }

Thanks for any help. I know this is not a working code snippet, but without the data connection this is tricky. I already tried the associative arrays, but was not able to understand how I can use in this example.

Thanks again, fj

0

2 Answers 2

4

Use an object, not an array.

var result = {};

Now to fill it you can just:

result[es] = result[es] + 1 || 1

And your for...in loop should work (but you should use .hasOwnProperty for sanity's sake).

for (var key in result) {        
    if (result.hasOwnProperty(key)) {    
        counter += result[key];

        if(counter > max){
             max = counter;
        }
    }
}

Your NaN result comes because you are doing this:

result[es] += 1;

Since result[es] is undefined (because you never assigned it a value), undefined + 1 is NaN (not a number).

Sign up to request clarification or add additional context in comments.

6 Comments

Also check if .hasOwnProperty
@domokun: True, I edited the answer to include that. I didn't want to confuse the issue, but it is worth mentioning.
This won't do the job without more work, because you can't sort object keys. Iterating using var key in result will not generate values in the correct order.
@TedHopp: It would be fairly trivial to transform the object into a sorted array when needed. It really depends on what's more important. Sorting performance or look up performance.
The problem OP seems to be struggling with is finding the maximum number of cars parked simultaneously. Your code doesn't do that. And I don't think it's "trivial" to transform the object into a sorted array that does the job.
|
0

You can't use a string as an index into an array; it amounts to using the array as an object. Even if you could, the logic is wrong because sorting an array sorts the values, not the indexes.

I suggest that you create an array of parking event objects and sort that using a custom comparison function. Something like this (untested):

var result = [];
var counter = 0;
var max = 0;

//SELECT enterTS, exitTS FROM parking;
// validated, that works

while (!rs.eof) {   
    var es = rs.fields(1).toString();
    var dd = rs.fields(2).toString(); // I'm assuming this is the exit time

    // create two events: one for entry and one for exit
    result.push({time: es, change: 1});
    result.push({time: dd, change: -1});
}

// sort based on event time
result.sort(function(a, b){ return a.time.localeCompare(b.time); });

// scan events, tracking current parking population
for (var key in result) {         
      counter += result[key].change;

      if(counter > max){
           max = counter;
      }
}

2 Comments

This, however, isn't "keyed" off date. Finding an entry for a particular date will be more difficult (although being sorted does help). The OP doesn't really provide enough context to determine how important that might be.
@MattBurland - In OP's question, the goal was to count how many parking events there were (a trivial problem, since that's just a count of records retrieved) and to find the maximum number of cars parked simultaneously. If look-up by date were important, then either another data structure would be needed (or, more likely, simply look up in the underlying db from which the data for this algorithm came).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.