# How can the following example be fully written out?

**URL:** <https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 21, 2019, 6:06pm UTC](https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707 "2019-01-21T18:06:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)\
**Post date:** [January 21, 2019, 6:06pm UTC](https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707/1 "2019-01-21T18:06:04Z")

</div>

**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 :

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

var dagens = beterData.results[i]; 

}

```

nested with another for-loop:

```auto

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 :

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d4f6918829ffeab97f0b6026b8a646abcf35e1d9.jpeg)

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 21, 2019, 6:15pm UTC](https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707/2 "2019-01-21T18:15:17Z")

</div>

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:

```auto
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:

```auto
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]);
...

```

---

<div class="post-metadata">

**Author:** ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)\
**Post date:** [January 21, 2019, 7:10pm UTC](https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707/3 "2019-01-21T19:10:51Z")

</div>

@Kevin

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

```auto
 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.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 21, 2019, 7:18pm UTC](https://discourse.processing.org/t/how-can-the-following-example-be-fully-written-out/7707/4 "2019-01-21T19:18:04Z")

</div>

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:

```auto
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));
  }
}

```
