Arrays and Dictionaries in JavaScript

1

I am doing an introductory course to the programming in which we are using JavaScript with the browser console and I was clear about the topic of the arrays but that of the dictionaries for nothing.

For example, this line of code I do not understand:

for(var i=0; i < cartas.length; i=i+1)

I understand that what it does is go through the array but I do not understand its operation.

This code does not understand either:

for (var i = 0; i < palos.length; i = i + 1) {        
    //aca me dice que para el indice igual a 0,i < palos.length esto no lo entiendo 
    //y el i=i + 1 lo que hace es recorrer el array 

    for (var j = 1; j <= 12; j = j + 1) {
        //esta parte no la entiendo para nada

        baraja[baraja.length] = { p: palos[ i ], v: j };
        //esta parte no la entiendo para nada

    }
}

For example, it is a subject that I have not been clear about. I appreciate if someone can help me.

    
asked by Fernando 31.07.2018 в 22:17
source

5 answers

0

In the first line you give, for(var i=0; i < cartas.length; i=i+1) its operation is like this: for is a reserved word of the language that waits for parentheses where you will define how the cycle will be done:

  

a) first receive a initialization , that is, define how the cycle will start ( var i=0 , declare a variable i whose value will be 0);

     

b) secondly what is the condition for the cycle to stop ( i<cartas.length , that variable i has to be less than the length of the array cartas );

     

c) finally receives the increment ( i=i+1 indicates that each time you finish doing what is inside the block for to that variable i will add 1).

That also responds to the first line of the code that you set as an example but changing the array cartas for the array palos . The following:

for (var j = 1; j <= 12; j = j + 1)

Indicates that you will initialize the variable j and assign it the value 1 , repeat the cycle while j is less than or equal to ( <= ) 12 and increase j at the end of the task will be one ( j=j+1 ).

Finally,

baraja[baraja.length] = { p: palos[ i ], v: j };

works like this:

  

The array baraja in the position baraja.length (the position in an array you place with the square brackets, [] ) will be an object whose property p will be what contains the fix palos in position i , as well as its property v will be the current value of j .

By parts:

  • In JavaScript you can define objects by just using the {} keys. Using this notation:

    {
      clave: valor
    }
    

    which is what you mean by dictionaries . In your example:

    {
      p: palos[i], // posiblemente "Palo"
      v: j         // posiblemente "Valor"
    }
    
  • Also in JavaScript, you can add a value to an array in several ways. For that matter, what you're doing is like this:

      

    If the array arr has 3 positions ( ["a", "b", "c"] ), its length is 3. Remember that its indexes ( positions ) start at 0, so both arr[0] = "a" .   If you assign a value in a position that does not exist (for example, arr[3] , remember that your last index is arr[2] ) then add it.

    (Remember that the comparison is with two == , including 3 === , while the assignment is with only one = ).

Therefore, when using the new index baraja.length you are creating a new element in the array, and that object will be a dictionary with a value for p and a value for v .

The latest JavaScript magic: you can access it in two ways:

baraja[1]["p"] // "Notación Corchetes"
baraja[1].p    // "Notación Punto"
    
answered by 31.07.2018 / 22:57
source
0

What you first say you do not understand for(var i=0; i < cartas.length; i=i+1) is called cycle for is one of the sentences you'll find in javascript that allows you to repeat x operation certain times, in this case it will start where i=0 , will continue executing what is inside its block until the variable i < cartas.length what is the same to have reached the limit of elements stored in the array cartas and the way to ensure that you reach the stop condition is increasing the value of the vairable i you can do it either with i++ or with i = i + 1 the second is the one of your example.

It means that if letters is an arrangement that has 10 elements with that sentence, you will execute what is next to the for block 10 times. This is the case with the other for only that the start condition varies as well as the stop condition

baraja[baraja.length] = { p: palos[ i ], v: j };

This means that in the shuffle arrangement you will be adding a new item of type dictionary, which will have as key or property p the value stored in the position i of the array sticks, and as a key or property v the current value of the variable j used in the cycle.

    
answered by 31.07.2018 в 22:28
0

Basically the for structure has three parts separated by a semicolon, these represent the initialization, the exit condition and the step.
The line for( var i = 0; i < cartas.length; i+=i+1) represents that in the next block the following will happen:

  • On entering you will execute the instruction var i = 0 which declares the variable i in the block and assign it the value 0 .
  • Evaluate the expression i < cartas.length , so that if it results be true the block will iterate, but if it turns out to be false then the program will exit the block and continue its normal execution.

  • And finally this step i += i + 1 this will be executed at the end of every iteration, the idea is to modify something, so that the block get to finalize its completion. That is, as it increases the value of i in 1 , possibly the condition i < cartas.length it will end up being false and the block will finish its execution.


  • Then, in the part of baraja[baraja.length] = { p: palos[ i ], v: j }; basically what you are doing is adding an element to the array, if the array has n elements, then your indexes go from 0 to n-1 , so that the element n will be created when is accessed (this is so in javascript, but in other languages it can cause many errors) . The property baraja.length says how many are n elements of the array baraja and when assigning a value, you are creating it.

        
    answered by 31.07.2018 в 22:32
    0

    This is simply a loop to repeat a line of code as many times as necessary, in this case it is used to go through all the spaces of your arrangement called letters

    for(var i=0; i < cartas.length; i=i+1)
    //i=0 es tu valor inicial 
    //i < cartas.length es la condicion que se tiene que cumplir para que deje de hacer lo que esta dentro de los corchetes {}
    //en este caso dice que la variable i tiene que ser menor al tamaño de tu arreglo cartas
    //i=i+1 lo puedes reemplazar por i++ que es el numero de veces que tu variable i va a incrementar en este caso cada que NO se cumpla la condicion i < cartas.length; aumentara en uno hasta que sean iguales 
    
        
    answered by 31.07.2018 в 22:34
    0

    Complementing the explanation of Daniel here is an example to better understand the operation of the for cycle:

    var palos = ["corazones", "picas", "tréboles", "diamantes"];
    var numeros = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 , 12];
    for (var i = 0; i < palos.length; i++) {
    for (var j = 0; j < numeros.length; j++) {
      console.log(numeros[j] + " de " + palos[i]);
        }
    }

    It is an example similar to the one you have shared, in this case there is a for cycle in which the variable i is equal to 0, less than the number of items in the array palos and is increasing from 1 in 1, and within this cycle there is another that works the same but with the array numeros , and with console.log thanks to the cycle for all cards are shown in a hypothetical deck in very few lines of code.

        
    answered by 31.07.2018 в 22:37