0

I am ultimately trying to create a function that generates some html based on values in a two dimensional(?) array. However, I am struggling to loop through all of the values. In the following code, it is the bottom else clause that my program never enters:

let food = [
  [
    'Wedges',
    ['Hero Wedge', "Lettuce, tomato, yada", '$19.42'],
    ['Differebt Wedge', "Chicken, tomato, yada", '$12.42'],
  ],
  [
    'Chicken',
    ['Chicken', "Lettuce, tomato, yada", '$19.42'],
    ['Brocolli Wedge', "Chicken, tomato, yada", '$12.42'],
  ]
]
generate(food);

function generate(food){
  for(i = 0; i < food.length; i++){
    for(j = 0; j < food[i].length; j++){
      if(j === 0){
        sectionName = food[i][j]; // "Wedges"
      }
      else{
        for(y = 0; y < food[i][j]; y++){
          console.log("were in this statment"); //Never runs
        }
      }
    }
  }
}

When i = 0 and j = 1 doesn't food[i][j] = ['Hero Wedge', "Lettuce, tomato, yada", '$19.42'] ? And since this is an array with 3 elements y < food[i][j] should evaluate to true? Thanks in advance.

2
  • 2
    y < food[i][j] -> y < food[i][j].length? Commented Jun 6, 2019 at 12:58
  • ..... you have no idea how long I was trying to figure out what was wrong before coming to stackoverflow. But thanks! Commented Jun 6, 2019 at 13:00

2 Answers 2

1

You need to check the length of the array and declare all variables.

for(y = 0; y < food[i][j].length; y++){ // use length

A better approach by iteration from one instead of zero and without a check.

function generate(food) {
    var i, j, y, sectionName;
    for (i = 0; i < food.length; i++) {
        sectionName = food[i][0];                     // assign outside of the loop
        console.log('sectionName', sectionName);
        for (j = 1; j < food[i].length; j++) {        // start from one
            for (y = 0; y < food[i][j].length; y++) { // use length
                console.log(food[i][j][y]);
            }
        }
    }
}

let food = [['Wedges', ['Hero Wedge', "Lettuce, tomato, yada", '$19.42'], ['Different Wedge', "Chicken, tomato, yada", '$12.42']], ['Chicken', ['Chicken', "Lettuce, tomato, yada", '$19.42'], ['Brocolli Wedge', "Chicken, tomato, yada", '$12.42']]];

generate(food);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

The for loop is ranging from y = 0 to y being less than the element at food[i][j] instead of the length of food[i][j].

Where food[i][j] is "wedges"

So, basically, your for loop would translate to for(y = 0; y < "wedges"; y++){

Hence, replace food[i][j] in the for with food[i][j].length, making the loop as

else{
    for(y = 0; y < food[i][j].length; y++) { //take the length of food[i][j]
       console.log("were in this statement"); //Now runs
    }
}

This should solve your issue.

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.