2

Can anyone explain why the 4th sub-array isn't working anymore? I believe because input[(i + 1)] is undefined? But it works for another... I'm a newbie and still learning how to figure out the best option.

function dataHandling(){


    for (var i=0;i < input.length   ; i++){
    for(var j=0; j < input[i].length; j++)
     /*
      if(j === 0){
         console.log("Nomor ID: "+ input[i][j] );
      }
      else if(j=== 1){
        console.log("Name: "+ input[i][j] );
      }
      else if(j=== 2){
        console.log("Birthplace n date: "+ input[i][j] +" " + input[i+1][j+1]);
      }
      else if(j=== 4){
        console.log("Hobby: "+ input[i][j] +"\n" );
      }
      */
     switch(j){
        case 0:
          console.log("Nomor ID: "+ input[i][j] );
          break;

          case 1:
          console.log("Name: "+ input[i][j] );
          break;

          case 2:
          console.log("Birthplace and date: "+ input[i][j] +" " + input[i+1][j+1]);
          break;

          case 3:
         // console.log("birthdate: "+ input[i][j] );
          break;

          case 4:
          console.log("Hobby: "+ input[i][j] +"\n" );
          break;

           default:
        break;
      }


    }

}

 var input = [
                ["0001", "Roman Alamsyah", "Bandar Lampung", "21/05/1989", "Reading"],
                ["0002", "Dika Sembiring", "Medan", "10/10/1992", "Playing Guitar"],
                ["0003", "Winona", "Ambon", "25/12/1965", "Cooking"],
                ["0004", "Bintang Senjaya", "Martapura", "6/4/1970", "Codding"]
              ];

dataHandling(input);

While it works for 1st-3rd arrays, it always errors in the 4th:

Nomor ID: 0003
Name: Winona
Birthplace n date: Ambon 6/4/1970
Hobby: Cooking

Nomor ID: 0004
Name: Bintang Senjaya
TypeError: input[(i + 1)] is undefined <<< 

I can understand if since the first i will error but only the 4th of i which not able to read the next sub-array. (Sorry for explaining with a newbie's way, still hard to explain with limited knowledge.)

1 Answer 1

2

First of all, I'd just like to say that you're doing great so far with learning Javascript. The error is you're trying to access the fifth array when i=4 and you use input[i+1][j+1]. Luckily, that isn't even a problem; what you want to do is access the same sub-array, but the next item, so only j should be increased by 1 (input[i][j+1]):

function dataHandling(){


    for (var i=0;i < input.length   ; i++){
    for(var j=0; j < input[i].length; j++)
     /*
      if(j === 0){
         console.log("Nomor ID: "+ input[i][j] );
      }
      else if(j=== 1){
        console.log("Name: "+ input[i][j] );
      }
      else if(j=== 2){
        console.log("Birthplace n date: "+ input[i][j] +" " + input[i+1][j+1]);
      }
      else if(j=== 4){
        console.log("Hobby: "+ input[i][j] +"\n" );
      }
      */
     switch(j){
        case 0:
          console.log("Nomor ID: "+ input[i][j] );
          break;

          case 1:
          console.log("Name: "+ input[i][j] );
          break;

          case 2:
          console.log("Birthplace and date: "+ input[i][j] +" " + input[i][j+1]);
          break;

          case 3:
         // console.log("birthdate: "+ input[i][j] );
          break;

          case 4:
          console.log("Hobby: "+ input[i][j] +"\n" );
          break;

           default:
        break;
      }


    }

}

 var input = [
                ["0001", "Roman Alamsyah", "Bandar Lampung", "21/05/1989", "Reading"],
                ["0002", "Dika Sembiring", "Medan", "10/10/1992", "Playing Guitar"],
                ["0003", "Winona", "Ambon", "25/12/1965", "Cooking"],
                ["0004", "Bintang Senjaya", "Martapura", "6/4/1970", "Codding"]
              ];

dataHandling(input);

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

Comments

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.