How can the following example be fully written out?

Slight heads-up: The following code was code from earlier in the day and doesn’t work properly, however, there is a small piece of code in there I would like to get some explanation of. Also sorry if the question is kinda vague.

In the following picture of (old)code I am working on the following:

  • Loading data from a certain JSON file
  • Look through the list of “results” that’s in there (Array), with another list “activity” in there.
  • I had to get another piece of info out of it, called “quantity”(not show in the pick. Should be behind -activiteit.quantity).

I was stuck on how to properly implement it and my teacher told me to use 2 for loops. The first for-loop, being :

for (i = 0; i < beterData.results.length; i++) {

var dagens = beterData.results[i]; 

}

nested with another for-loop:



for (i = 0; i < dagens.activity.length; i++) {

var activiteit= dagens.activity[j];

I understand that my teacher created local vars (dagens + activiteit) to make the code a bit shorter to use, however, I do not know how to fully write it out.

is it for instance like this :

for (i = 0; i < beterData.results.length; i++) {

beterData = beterData.results[i]; 

}

or something different?

Do not get me wrong, I understand that this makes the code easier to follow. I do not plan to want to write it out just for the sake of putting it like that in my code, but to understand what is happening.

If anyone could tell me how the code would look like when fully written out, and maybe give some explanation, that would be nice. If my question is unclear, please tell me what you do not get and I will try to explain it as best as i can.

}

23

1 Like

Try adding more console.log() statements to figure out what’s going on. Print out the value of i, dagens, j, and activiteit each iteration of the loop. You might also want to use more descriptive variable names, as i and j can be a bit confusing.

But you can think about it this way: your outer loop iterates over your results. Then for each result, your inner loop iterates over the activities of that result.

For example, let’s say I have an array of books, and each book contains an array of chapters. To iterate over every chapter in every book, I might do something like this:

for(let bookIndex = 0; bookIndex < library.books.length; bookIndex++){
  const book = library.books[bookIndex];
  for(let chapterIndex = 0; chapterIndex < book.chapters.length; chapterIndex++){
    readChapter(book.chapters[chapterIndex]);
  }
}

If you wrote it out without loops, it would look something like this:

readChapter(library.books[0].chapters[0]);
readChapter(library.books[0].chapters[1]);
readChapter(library.books[0].chapters[2]);
readChapter(library.books[1].chapters[0]);
readChapter(library.books[1].chapters[1]);
readChapter(library.books[1].chapters[2]);
readChapter(library.books[2].chapters[0]);
...
1 Like

@Kevin

Correct me if I am wrong, but would it that make it the following? :

 for (i = 0; i < beterData.results.length; i++) {

   beterData = beterData.results[i]; 

      for (j = 0; j < beterData.results.activity.length; j++ {
          
            beterData = beterData.results.activity[j]
   }

}

If not the i am unfortunately stuck on this.

You’re reusing the same variable beterData in several places, which I don’t think is what you want to do.

I don’t know what your code is supposed to do, but I would start by adding more print statements and using different variables. Something like this:

for (let resultIndex = 0; resultIndex < beterData.results.length; resultIndex++) {
  console.log('resultIndex: ' + resultIndex);

  let result = beterData.results[i];
  console.log('result: ' + JSON.stringify(result));

  for (let activityIndex = 0; activityIndex < result.activity.length; activityIndex++) {
    console.log('activityIndex: ' + activityIndex);
    let activity = result.activity[activityIndex];
    console.log('activity: ' + JSON.stringify(activity));
  }
}
1 Like