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