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: None. that is, none of the above. The options were a, b, c, but none of these variables contains an array of randomly generated or selected numbers. Let's see why.
The instruction let a = Math.random([0,1,2]) does not make correct use of the Math object's random function. This function has no parameters, so it ignores whatever you supply it with. This code might make more sense in other languages, but not in Javascript. Did you get tricked by this?
On the other hand, the instruction let b = Math.floor(Math.random()) * 3 makes correct use of Math.random, but it only generates one random number, which is rounded down to zero and multiplied by 3, resulting in the number 0. If you know Javascript, you will know that this is a correct, but meaningless usage. Math.floor also doesn't create an array, and even if it did, you can't multiply by 3 or who knows what you get.
Finally, the line let c = Array(3).map(e => Math.random()) may be more misleading. The first part Array(3) does indeed create an array of size 3, but it is a sparse array. This type of array has empty slots, which is different from containing undefined elements. As a consequence, many standard methods for array objects do not work as expected. The map method, for example, will skip the empty slots and that explains why the instruction in our code does not generate random numbers either. It simply won't execute.
Did you fall for any of these tricks or are you sure you know how to do it?
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: Ninguna, es decir, ninguna de las anteriores. Las opciones eran a, b, c, pero ninguna de estas variables contiene un arreglo de n煤meros seleccionados o generados aleatoriamente. Veamos por qu茅.
La instrucci贸n let a = Math.random([0,1,2]) no hace un uso correcto de la funci贸n random del objeto Math. Esta funci贸n no tiene par谩metros, por lo que ignora todo lo que le proporciones. Este c贸digo podr铆a tener m谩s sentido en otros lenguajes, pero no en Javascript. 驴Te enga帽贸 esto?
Por otro lado, la instrucci贸n let b = Math.floor(Math.random()) * 3 hace un uso correcto de Math.random, pero solo genera un n煤mero aleatorio, que se redondea a cero y se multiplica por 3, lo que da como resultado el n煤mero 0. Si conoces Javascript, sabr谩s que se trata de un uso correcto, pero sin sentido. Math.floor tampoco crea un arreglo, e incluso si lo hiciera, no puedes multiplicar por 3 o qui茅n sabe qu茅 obtendr谩s.
Finalmente, la l铆nea let c = Array(3).map(e => Math.random()) puede ser m谩s enga帽osa. La primera parte Array(3) de hecho crea un arreglo de tama帽o 3, pero es un arreglo dispersa (sparce array). Este tipo de arreglo tiene espacios vac铆os, lo cual es diferente de contener elementos undefined. Como consecuencia, muchos m茅todos est谩ndar para objetos tipo Array no funcionan como se esperaba. El m茅todo map, por ejemplo, omitir谩 los espacios vac铆os y eso explica por qu茅 la instrucci贸n en nuestro c贸digo tampoco genera n煤meros aleatorios. Simplemente no se ejecutar谩.
驴Ca铆ste en alguno de estos trucos o est谩s seguro de saber c贸mo hacerlo?
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.