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: 1. Line 1 declares and initializes both an empty data object and a list array with 2 as the single item; nothing special.
Line 2 creates a constant v from the ternary operator and this is where the interesting part of the quiz happens. In javascript, objects are evaluated in a logical expression as truthy even if they are empty. This is a fundamental difference with Python, where the opposite is true: an empty dictionary will be considered falsy. As a result, the value of v is 1.
The same thing could happen on line 3, in the if logical expression: both v and list have truthy values, but even if the list array were empty, it would also logically evaluate to truthy, which again differs from Python, where empty lists are considered falsy.
Therefore, that condition on line 3 is truthy, so the block on line 4 is executed, obtaining 1 as the output of the script.
You may already know this difference between Javascript and Python, but if not, then you are learning. I learned it the hard way. Congratulations to @dobro2020 for hitting the correct output. Here we have the explanation for this behavior of the program.
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: 1. Las l铆nea declara e inicializa tanto un objeto data vac铆o como un objeto list con 2 como 铆tem 煤nico; nada especial.
La l铆nea 2 crea una constante v a partir del operador ternario y aqu铆 es donde ocurre lo interesante del quiz. En javascript, los objetos son evaluados en una expresi贸n l贸gica como verdaderos (truthy) as铆 est茅n vac铆os. Esto es una diferencia fundamental con Python, donde ocurre lo opuesto: un diccionario vac铆o ser谩 evaluado como falso (falsy). Como consecuencia, el valor de v es 1.
Lo mismo podr铆a ocurrir en la l铆nea 3, en la expresi贸n l贸gica del if: tanto v como list tienen valores verdaderos, pero a煤n si list estuviera vac铆o, tambi茅n ser铆a evaluado l贸gicamente como verdadero, lo que de nuevo se diferencia de Python, donde las listas vac铆as son consideradas falsy.
De tal manera, que esa condici贸n de la l铆nea 3 es verdadera, por lo que se ejecuta el bloque en la l铆nea 4, obteniendo 1 como salida del script.
Puede que ya conocieras esta diferencia entre Javascript y Python, pero si no, pues est谩s aprendiendo. Yo lo aprend铆 de la manera dif铆cil. Felicidades a @dobro2020 por acertar con la salida correcta. Aqu铆 tenemos la explicaci贸n a este comportamiento del programa.
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.