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: c5 As usual, let's look at the script line by line. Line 1 declares a variable zz with the string 'Nf6'. If you know chess and have followed this series before, you will probably recognise that it describes a chess move in algebraic notation. The same is true for the game list in line 2.
Line 3 creates a new moves list from the game list and the variable zz. This list consists of [ 'Nf3', 'c5', 'c4', 'Nf6' ].
The interesting thing, however, occurs in line 4: const one = moves.reduce((a, b) => a > b ? a : b). We have created a variable one by applying the built-in reduce method, which produces a single value from the values of a given iterable, which in our case is moves. The function that reduce receives is the one that does the trick. In our case, the function (a, b) => a > b ? a : b selects the largest value (the ternary operator says it all).
Therefore, reduce will get the largest value in the moves list, which is c5. Why c5? Well, we are doing lexicographic comparison. Let's look at the contents of moves again: [ 'Nf3', 'c5', 'c4', 'Nf6' ]. Upper case letters have a lower ordinal value than lower case letters, so they are considered lower. That eliminates Nf3 and Nf6. We are left with c5 and c4, and here it is easy to tell that c5 is greater by comparing the second character of both strings.
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: c5. Como de costumbre, veamos el script l铆nea por l铆nea. La l铆nea 1 declara una variable zz con la cadena 'Nf6'. Si sabes ajedrez y has seguido esta serie antes, probablemente reconocer谩s que describe una jugada de ajedrez en notaci贸n algebraica. Lo mismo ocurre con la lista game en la l铆nea 2.
La l铆nea 3 crea una nueva lista de moves a partir de la lista de game y la variable zz. Esta lista consta de [ 'Nf3', 'c5', 'c4', 'Nf6' ].
Lo interesante, sin embargo, ocurre en la l铆nea 4: const one = moves.reduce((a, b) => a > b ? a : b). Hemos creado una variable one aplicando el m茅todo incorporado reduce, que produce un valor 煤nico a partir de los valores de un iterable dado, que en nuestro caso es moves. La funci贸n que recibe reduce es la que hace el truco. En nuestro caso, la funci贸n (a, b) => a > b ? a : b selecciona el valor m谩s grande (el operador ternario lo dice todo).
Por lo tanto, reduce obtendr谩 el valor m谩s grande en la lista de moves, que es c5. 驴Por qu茅 c5? Bueno, estamos haciendo una comparaci贸n lexicogr谩fica. Veamos de nuevo el contenido de las moves: [ 'Nf3', 'c5', 'c4', 'Nf6' ]. Las letras may煤sculas tienen un valor ordinal m谩s bajo que las min煤sculas, por lo que se consideran inferiores. Eso elimina Nf3 y Nf6. Nos quedamos con c5 y c4, y aqu铆 es f谩cil saber que c5 es mayor comparando el segundo car谩cter de ambas cadenas.
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.