The output doesn't print all array numbers

The output doesn't print all array numbers
0

#1

hello,

I try to print all the array’s properties but it doesn’t come out.

here is what i try:

 var myarr = [[1,2],[3,4],5,6];
        
        for (i=0; i<myarr.length; i++){
            for(j=0; j<myarr[i].length; j++){
               for(a=0; a<myarr[i][j].length; a++){
                   console.log(myarr[i][j][a]);
                   
               } 
                
                
            }
          
        }


#2

Out of curiosity, did you mean for your myarr to look like:

var myarr = [[1,2],[3,4],[5,6]];

instead of

var myarr = [[1,2],[3,4],5,6];


#3

Hey Randell,

that is what i meant:

[[1,2],[3,4],5,6];

an array with 4 properties.

It doesn’t print anything.


#4

Is your goal to print each number regardless if it is nested in a subarray?


#5

yes that’s what i try


#6

See below where I took your solution and added some console.log statements. If you run the code below, you can see there are times when the length is undefined. When that happens, the next for loop will not be entered, because the for loop condition will evaluate to false.

for (i=0; i<myarr.length; i++){
  console.log('i = '+i)
  console.log('myarr['+i+'].length = '+myarr[i].length)
  for(j=0; j<myarr[i].length; j++){
    console.log('j = '+j)
    console.log('myarr['+ i + '][' + j + '].length = ' + myarr[i][j].length)
    for(a=0; a<myarr[i][j].length; a++){
      console.log(myarr[i][j][a]);
    } 
  }
  console.log('') // break to show start of new outer loop iteration
}

Assuming the original array would only be made up of single elements (like the 5 and 6 in your example array) or nested single dimensional arrays such as the [1,2] and the [3,4], the following code would display each element in the console.

var myarr = [[1,2],[3,4],5,6];
        
for (i=0; i<myarr.length; i++){
  if (Array.isArray(myarr[i])) {
    for(j=0; j<myarr[i].length; j++){
      console.log(myarr[i][j]);
    }
  }
  else {
    console.log(myarr[i]);
  }
}

#7

thanks randell,

by adding the array.isarray method you help the machine to validated that myarr[i] is an array, then the j can run. Did i get it right?


#8

You are correct. If the element is an array, then it will iterate through it to display each element of the subarray.


#9

ok, so why in this example total is equal to 9 instead of 15?

var myarr = [1,2,3,[4,5]];
        
        var total = 0;
        
        for (i=0; i<myarr.length; i++){
            if (Array.isArray(myarr[i])){
            for (j=0; j<myarr[i].length; j++){
                
            
            total += myarr[i][j];
            }
        }
            else {
                console.log("no");
            }
        }
        console.log(total);


#10

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

You were using 3 single quote marks instead of 3 backticks to format your code.

Why do you think it should equal 15 instead of 9? Since the first three elements are not arrays, your else block of code executes. Your else block prints “no” to the console and does not change the value of total.


#11

i understand now, thanks! by the way, i can’t find the backsticks in my mac. it used to be in older modals next to the 1 key, but now it is not.