My Coding Quiz #25

in #hive-19638711 months ago

My Coding Quiz #25 👨‍💻🛠️🧩

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


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

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

Sort:  

Yo voy por la 4ta opción Synthax Error, ya que me parece que el +-+- va a dar error, estaré pendiente a ver cual es la solución.

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.