Forum Moderators: open

Message Too Old, No Replies

Why doesn't my code see the inner set of a 2 dim array?

It objects that seats[l].length is not an object.

         

Baruch Menachem

9:16 pm on Dec 28, 2009 (gmt 0)

10+ Year Member



I have a two dimensional array called seats. (This is being sort of copies out of a book. The book has the solution, but my solution looks identical to me, except for the order)

the outer loop is a while loop, with l as the counter, the inner loop looks like "

  

var done=false;
var l=0;

//search through all seats for availabiltiy
while (! done && l<seats.length)
{
for (var il=0; il < seats[l].length; il++)

Anyway, I get an error that seats[;].length is not an object, an I can't see the error here

Thanks

Fotiman

10:01 pm on Dec 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You while loop will continue until both done == true AND l < seats.length. Therefore, if you reach the end of the seats array and you've not set done = true, then it will continue to loop, causing an out of index error. Not sure if that's the problem you're seeing, but it certainly looks suspect to me.

Baruch Menachem

11:18 pm on Dec 28, 2009 (gmt 0)

10+ Year Member



I thought && required both to be true, not just one. Therefore, when you reach the end of the index, it should hit false and go to the next row in the array...?

daveVk

11:59 pm on Dec 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

while ( (! done) && ( l<seats.length ) )

use brackets liberally to make intent clear

make sure l is initialized and incremented

Baruch Menachem

12:33 am on Dec 29, 2009 (gmt 0)

10+ Year Member



I think I found the issue. You were right, l's increment is outside the loop

Fotiman

3:22 pm on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm sorry, you were right. Not sure what I was thinking. :)