Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: [ '0', '1', '2' ]. Line 1 simply declares and initializes an array res to empty and a variable a to false.
Line 2 begins the definition of a for block like this: for (const a in [1,3,4]). As we see, it is expected to iterate over the explicit array, assigning its values to the constant a. This is not a problem, since the constant a is destroyed and recreated on each iteration, so there is no error related to trying to modify a constant.
Similarly, that constant is local to the for, so it is different from the variable a that we declared in the first line. It should not cause us confusion.
What needs to be noted is that in the for header we did not use the of operator but rather in and both behave differently. When in is applied to an array, it loops through its indexes, while of loops through its values. This is a fundamental difference with Python that programmers should remember: in Javascript you have to use of instead of in if you want to iterate over values of an array.
This explains why the array res is composed of [ '0', '1', '2' ] instead of [ '1', '3', '4' ]. I hope you found these details interesting.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: [ '0', '1', '2' ]. La l铆nea 1 simplemente declara e inicializa un array res como vac铆o y una variable a a false.
La l铆nea 2 comienza la definici贸n de un bloque for as铆: for (const a in [1,3,4]). Como vemos, se espera que itere sobre el array expl铆cito, asignando sus valores a la constante a. Esto no es un problema, puesto que la constante a se destruye y recrea en cada iteraci贸n, por lo que no produce error relacionado con intentar modificar una constante.
Similarmente, esa constante es local al for, por lo que es diferente de la variable a que declaramos en la primera l铆nea. No debe causarnos confusi贸n.
Lo que s铆 hay que detallar es que en el encabezado del for no usamos el operador of sino in y ambos se comportan de manera diferente. Cuando in es aplicado a un array, recorre sus 铆ndices, mientras que of recorre sus valores. Esta es una diferencia fundamental con Python que los programadores deben recordar: en Javascript hay que usar of en vez de in si quieres iterar sobre los valores de un array.
Eso explica porqu茅 el arreglo res se compone de [ '0', '1', '2' ] en vez de [ '1', '3', '4' ]. Espero que estos detalles te hayan parecido interesantes.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.