cycle for javascript, jquery

1

what would the syntax of a nested for cycle look like to traverse a 7x7 matrix and randomly attach a series of images?

for the random part create a variabe of Math.random, which is not where in the syntax to use.

I also know that I have to have two elements or variables (i, j) that one would be the columns and another one would be the rows.

I'm starting to study programming It would be a pleasure that you could explain to me and why?

    
asked by rlence 23.09.2018 в 11:32
source

1 answer

0

Welcome to SOe!

Matrices are usually two-dimensional, like the one you describe. Going through a matrix is simple, the for loops are like this:

for(let i=0; i < cantidadFilas; i++){ *
 for(let j=0; j < cantidadElementosXFila; j++){ **
  //aquí insertas tu imagen aleatoria
 }
}

* Where the number of rows is 7 in your case, this loop will jump only from row to row, over the 7 rows, without entering each of its elements

** This loop will go through each iteration of the top loop, all the elements of the row in which it is, at that moment, for example when i = 0, it will be the iteration on the first row, being this second loop will travel your 7 elements (when j = 0, your 1st element, when j = 1, your second element, and so on).

NOTE : The notation of the asterisks is only used to refer to the explanation of each loop and contribute to the cleaning of the code used as an example, you should not include it in your code.

You have a nice source of information to delve into a for loop here: MDN : Mozilla Developer Community

Feel free to participate and ask in this community as well as contribute to it whenever you want, respecting the clear rules.

    
answered by 23.09.2018 / 12:10
source