Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a two multidimensional arrays $path and $record i have to do looping and update the array values there is some issue with array declarations i am not able to find out.

Console Error: Uncaught TypeError: Cannot read property '0' of undefined at the line if($record[$path[a][icell]][j]){

$record has been assigned values before like in the lines $record[i][2] = true; same is with $path but values are retrived from array not assigned just 5 lines before the line error occurs that is $path = [[$start]]; so why the error occurs when both the arrays are used combinedly if($record[$path[a][icell]][j]){.

if u require jsfiddle i will provide but i guess it doesn't serve the purpose contains lot of other irrelevant scripts too so unnecessarily making the code overwhelming. Hope i made myself as clear as possible u need any clarity i will sure provide

JQUERY:

var $record = new Array();
  $(".solve button").on("click",function(){
    if(($start+1)&&($end+1)){    
      var i=0,
          x,y;

      $("td").each(function(){
          if(!$.isArray($record[i])) { $record[i] = []; }
              if(!$(this).hasClass("top")){
                  if($(this).css("border-top-color") == "rgb(0, 0, 0)"){
                    $record[i][0] = true;
                  }
              } else { $record[i][0] = false; }
              if(!$(this).hasClass("right")){
                  if($(this).css("border-right-color") == "rgb(0, 0, 0)"){
                      $record[i][1] = true;
                  }
              } else { $record[i][1] = false; }
              if(!$(this).hasClass("bottom")){
                  if($(this).css("border-bottom-color") == "rgb(0, 0, 0)"){
                      $record[i][2] = true;
                  }
              } else { $record[i][2] = false; }
              if(!$(this).hasClass("left")){
                  if($(this).css("border-left-color") == "rgb(0, 0, 0)"){
                      $record[i][3] = true;
                  }
              } else { $record[i][3] = false; }
              i++;
          });
          //console.log("record[2][0] : "+$record[2][0]);    
          } else {
              $("#both").fadeIn("fast",function(){
                  $("#both").delay(2000).fadeOut();
              });
          }

          /* finding paths */

          var $path = new Array(),  // $path[path_index][cell_index]
              ipath=0,icell=0,
              fresh=old=0;
              $path = {};
          if(!$.isArray($path[0])) { $path[0] = []; }
          $path = [[$start]];
          for(i=0;i<$("#maze td").length;i++){      
              for(j=0;j<4;j++){
                    for(a=0;a<=ipath;a++){
                        icell = $path[a][$path[a].length-1];
                        if(icell+1){
                            if($record[$path[a][icell]][j]){
                            old=fresh;
                            if(!$.isArray($path[a])) { $path[a] = []; }
                            if(j=0){
                                for(k=0;k<=icell;k++){
                                    $path[a][k] = $path[a-1][k];
                                }
                                $path[a][icell+1] = $path[a][icell] - $cols; // moving             top
                                ipath++;
                                } else if (j=1){
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] + 1;  // moving                     right
                                    ipath++;
                                } else if (j=2){
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] - $cols; // moving bottom                           
                                    ipath++;
                                } else {
                                    for(k=0;k<=icell;k++){
                                        $path[a][k] = $path[a-1][k];
                                    }
                                    $path[a][icell+1] = $path[a][icell] - 1;  // moving                     left
                                    ipath++;
                                }
                            }
                        }
                    }
                }
            }
      });
share|improve this question
1  
Honestly, I would bin this and use an array of objects. They are much easier to code and maintain. –  Rory McCrossan Apr 28 at 19:02
    
@RoryMcCrossan what do u mean by array of objects –  Sagi_Avinash_Varma Apr 28 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.