My Coding Quiz #43

avatar

My Coding Quiz #43 👨‍💻🛠️🧩

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: Holy cow. First a function guess is defined that takes a parameter what and uses it in a try...catch clause, which is the crux of the matter. However, its behaviour makes sense depending on the argument received, which is null as we see at the end, when the function is called.

First we try to apply the toString() method to what, which contains null. However, this will throw a TypeError, since null has no properties, so even if you want to, you can't convert it to a string, at least not in this way. Of course, the other part of the instruction, that is, || "Oh boy" is not executed and the program flow goes to catch.

Inside the catch block we have the instruction return "No way". Should this be what is returned by the function? Actually, if your entire clause has a finally block, it will execute no matter what you have in your previous blocks. This is not a bug, it's a feature, and it's true in many programming languages. Since the finally block has a return, it will overwrite the previous returns. That explains why Holy cow is what is printed at the end.

Remember this behaviour when coding your functions with the try...catch clause. If you ignore it as a feature, it could lead to a headache and bad code.


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 #43 👨‍💻🛠️🧩

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: Holy cow. Primero se define una función guess que toma un parámetro what y lo usa en una cláusula try...catch, que es el meollo del asunto. Sin embargo, su comportamiento tiene sentido dependiendo del argumento recibido, que es null como vemos al final, cuando se llama a la función.

Primero intentamos aplicar el método toString() a what, que contiene null. Sin embargo, esto generará un TypeError, ya que null no tiene propiedades, por lo que incluso si lo deseas, no puedes convertirlo en una cadena de caracteres, al menos no de esta manera. Por supuesto, la otra parte de la instrucción, es decir, || "Oh boy" no se ejecuta y el flujo del programa pasa a catch.

Dentro del bloque catch tenemos la instrucción return "No way". ¿Debería ser esto lo que devuelve la función? En realidad, si toda tu cláusula tiene un bloque finally, se ejecutará sin importar lo que tengas en tus bloques anteriores. Esto no es un bug, es una característica, y pasa también en muchos lenguajes de programación. Dado que el bloque finally tiene un return, este sobrescribirá los returns anteriores. Eso explica por qué Holy cow es lo que es impreso al final.

Recuerda este comportamiento cuando codifiques tus funciones con la cláusula try...catch. Si lo ignoras como una característica, podrías terminar con un dolor de cabeza y un código incorrecto.


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.



0
0
0.000
3 comments
avatar

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. 
 

0
0
0.000