My Coding Quiz #12

My Coding Quiz #12 馃懆鈥嶐煉火煕狅笍馃З

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: 1. For starters, all the code consists of a self-executing function, which is very normal in Javascript. We recognize the self-executing function by being wrapped in parentheses followed by the parentheses corresponding to the call. When designing code, these functions are generally anonymous, so the identifier quiz was unnecessary.

Let's move on to its content. In the first line of the quiz function, a y variable is declared and assigned the value false. Nothing special.

Then there is a if (y % 2 === 0) clause. Perhaps not surprisingly, this expression evaluates to true. The point is that in Javascript booleans are converted to integers depending on the operator used on them and we know that 0 % 2 is 0.

There's a theory going around that Booleans are really integers behind the scenes, but official documentations don't talk about it at all, but about type conversions.

This explains what happens in the if block (line 4) that happens to be executed. The false value of y is converted to the integer 0 and this is incremented by one by the ++ operator, overriding the value of y. The value of y is therefore 1 and that is the output that is logged on the final line of the function.

There is no reason to believe that the code has a bug or syntax error. It is a feature of Javascript and you have to keep it in mind when writing good code, to avoid errors.


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 #11 馃懆鈥嶐煉火煕狅笍馃З

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: 1. Para empezar, todo el c贸digo consiste en una funci贸n autoejecutable, lo cual es muy normal en Javascript. Reconocemos la funci贸n autoejecutable por estar envuelta en par茅ntesis seguida de los par茅ntesis correspondiente a la llamada. A la hora de dise帽ar c贸digo, estas funciones son generalmente an贸nimas, de modo que el identificador quiz era innecesario.

Pasemos a su contenido. En la primera l铆nea de la funci贸n quiz se declara una variable y y se le asigna el valor false. Nada especial.

Luego, hay una cl谩usula if (y % 2 === 0). Quiz谩 no sorprenda que esta expresi贸n se eval煤e como verdadera. El punto est谩 en que en Javascript los booleanos son convertidos a enteros dependiendo del operador usado en ellos y sabemos que 0 % 2 es 0.

Anda circulando una teor铆a seg煤n la cual, los booleanos son realmente enteros tras bastidos, pero documentaciones oficiales no hablan de ello para nada, sino de conversiones de tipos.

Esto explica qu茅 ocurre en el bloque del if (l铆nea 4) que pasa a ser ejecutado. El valor false de y es convertido al entero 0 y este es incrementado en uno por el operador ++, sobreescribiendo el valor de y. El valor de y, por tanto, es 1 y esa es la salida que se registra en la l铆nea final de la funci贸n.

No hay razones para pensar que el c贸digo posee un bug o error de sintaxis. Es una caracter铆stica de Javascript y hay que tenerla en mente a la hora de escribir buen c贸digo, para evitar errores.


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
5 Comments
Ecency