# Array help for using two for loops

**URL:** <https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289>\
**Category:** General\
**Created:** [December 14, 2019, 9:07pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289 "2019-12-14T21:07:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoster181](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@ghoster181](https://forum.freecodecamp.org/u/ghoster181)\
**Post date:** [December 14, 2019, 9:07pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/1 "2019-12-14T21:07:11Z")

</div>

```auto
var cars = [["A", "B", "C"],["D", "E", "F"],["G", "H", "I"]];
var diag = (cars.length*2) - 1
var result = [];

  for(i = cars.length - 1; i >= 0; i--) {
    for(j = cars.length - 1; j >= 0; j--) {
      const x = i;
      const y = diag - j;
      
        if( x>= 0 && y<= diag) {
            console.log(result)
        }
    }

```

Can someone help with this? Trying to start from bottom right to top left. I know I need a function too.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [December 14, 2019, 9:11pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/2 "2019-12-14T21:11:31Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

**Note:** Backticks are not single quotes.

![markdown_Forums](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![snowmonkey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snowmonkey/32/241579_2.png) [@snowmonkey](https://forum.freecodecamp.org/u/snowmonkey)\
**Post date:** [December 14, 2019, 9:12pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/3 "2019-12-14T21:12:12Z")

</div>

So your `j` loop, which I assume you want to loop over the INNER arrays, should probably not be comparing to `cars.length`. Instead, at the first iteration (when `i==0`), you want to be setting `j` to `cars[0].length-1`. At the next iteration, when `i==1`, you want `j` to equal `cars[1].length-1`.

Each sub-array is found at a given index in the `cars` array. Can you think of a way, inside that inner loop, to get that? Hint: think about `i`.

---

<div class="post-metadata">

**Author:** ![ghoster181](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@ghoster181](https://forum.freecodecamp.org/u/ghoster181)\
**Post date:** [December 14, 2019, 9:49pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/5 "2019-12-14T21:49:50Z")

</div>

```auto
[["A", "B", "C"],
["D", "E", "F"],
["G", "H", "I"]];

[I, F, H, C, E, G, B, D, A];

```

Trying to get it to read like that. From bottom right to top left.

---

<div class="post-metadata">

**Author:** ![ghoster181](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@ghoster181](https://forum.freecodecamp.org/u/ghoster181)\
**Post date:** [December 14, 2019, 9:54pm UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/7 "2019-12-14T21:54:26Z")

</div>

Because it is going from bottom right to top left and that is the most bottom right after it cycles thru the array that forms a square.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:30am UTC](https://forum.freecodecamp.org/t/array-help-for-using-two-for-loops/333289/9 "2021-01-16T11:30:23Z")

</div>

Closed on January 16, 2021, 11:30am UTC


