My Coding Quiz #23

My Coding Quiz #23 👨‍💻🛠️🧩

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.

Without further ado, here's the riddle...




Quiz
By @eniolw


What's your choice?

Solution to the previous quiz: undefined. Line 1 creates a new array a using the good, old function Array. Since two arguments were specified, the function will assume them as the elements of the array.

Line 2 also creates an array with the same special function, but by specifying a single numeric type argument, it understands it as the length of the array and will create it with undefined elements . As given in the code, we then have an array of three elements, but these are all undefined.

Line 3 applies the concat method to join the content of b with a, without modifying a , but creating a new array.

In the same line, a search is done with the find method where we want to obtain an item that is equal to 3. That item does not exist in the given array because, as we already saw, it could only be formed by the defined elements 1 and 2 and undefined elements, while the value 3 on line 2 is just a length, not an element.

The important thing to know is that when find does not get a match, what is returned is undefined, the most appropriate value that is opposed to a defined value, which is what was expected to be obtained.

This detail is important to know because although they are similar, undefined, null and false are different and our code may contain bugs if we do not appreciate these differences.


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, Hive Learners and others.


Mi Quiz de Programación #23 👨‍💻🛠️🧩

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é.

Sin más preámbulos, he aquí el acertijo...




Quiz
Por @eniolw


¿Cuál es tu elección?

Solución al quiz anterior: undefined. La línea 1 crea un nuevo arreglo a usando la vieja función Array. Como se especificaron dos argumentos, la función los asumirá como los elementos del arreglo.

La línea 2 también crea un arreglo con la misma función especial, pero al especificársele un solo argumento de tipo numérico, esta lo entiende como la longitud del arreglo y lo creará con elementos indefinidos. Como está dado en el código, tenemos entonces un arreglo de tres elementos, pero estos son undefined todos.

La línea 3 aplica el método concat para unir el contenido de b con a, sin modificar a, sino creando un nuevo arreglo.

En la misma línea se hace una búsqueda con el método find donde queremos obtener un item que sea igual a 3. Ese ítem no existe en el arreglo dado porque, como ya vimos, solo podría estar formado por los elementos definidos 1 y 2 y elementos indefinidos, mientras que el 3 de la línea 2 es solo una longitud, no un elemento.

Lo importante es saber que cuando find no obtiene una coincidencia, lo que regresa es undefined, el valor más apropiado que se contrapone a un valor definido, que es lo que se esperaba obtener.

Este detalle es importante saberlo porque aunque son similares, undefined, null y false son distintos y nuestro código puede contener bugs si no apreciamos esas diferencias.


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, Hive Learners y otras.

H2
H3
H4
3 columns
2 columns
1 column
4 Comments