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: number. Line 1 creates a string that can be easily interpreted as a date.
Then we create three variables using the powerful destructuring assignment feature of JavaScript with which we assign to several variables the contents of an array or object, in this case the array created by the split method. The variables m, d and y have the values '11', '30', '2023' respectively.
We then initiate a series of nested try-catch clauses. One more level of nesting would make programmers like Linus Torvalds insult you, ha ha. Let's examine them more closely.
console.log(typeof Float(y)) cannot be executed because the Float function or class does not exist in JavaScript in this form. Did you know that?
console.log(typeof(+y)) does run because it is valid code. The parentheses for the typeof operator are unnecessary here, but they are not a syntax error. The key is that the + operator placed in front of a variable like this will force or attempt a type conversion if the type of the variable is not numeric already. As a consequence, the value of y will be converted to number type and we will get that as output.
console.log(typeof(y)) would not be able to be executed in this script, but it is valid code as well.
The crux of this quiz, therefore, is to know the potential of the unary operator + to do these data type conversions. It is difficult to consider it a hack, since it is in fact the method recommended by MDN and other specialized sites, which consider it the "fastest" and the "preferred" one, which in turn makes ParseInt and other similar conversion methods obsolete.
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: number. La l铆nea 1 crea una cadena que puede interpretarse f谩cilmente como una fecha.
Luego creamos tres variables usando la potente funcionalidad de asignaci贸n destructuring de JavaScript con la que asignamos a varias variables el contenido de un array u objeto, en este caso el array creado por el split m茅todo. Las variables m, d y y tienen los valores '11', '30', '2023' respectivamente.
Luego iniciamos una serie de cl谩usulas try-catch anidadas. Un nivel m谩s de anidamiento har铆a que programadores como Linus Torvalds te insultaran, ja, ja. Examin茅moslos m谩s de cerca.
console.log(typeof Float(y)) no se puede ejecutar porque la funci贸n o clase Float no existe en JavaScript de esta manera. 驴Lo sab铆as?
console.log(typeof(+y)) se ejecuta porque es un c贸digo v谩lido. Los par茅ntesis para el operador typeof son innecesarios aqu铆, pero no constituyen un error de sintaxis. La clave es que el operador + colocado delante de una variable as铆 forzar谩 o intentar谩 una conversi贸n de tipo si el tipo de la variable a煤n no es num茅rico. Como consecuencia, el valor de y se convertir谩 a un tipo num茅rico y lo obtendremos como resultado.
console.log(typeof(y)) no podr铆a ejecutarse en este script, pero tambi茅n es un c贸digo v谩lido.
El meollo de este quiz, por lo tanto, es conocer el potencial del operador unario + para realizar estas conversiones de tipos de datos. Es dif铆cil considerarlo un hack, ya que es en realidad el m茅todo recomendado por MDN y otros sitios especializados, que lo consideran el "m谩s r谩pido" y el "preferido", lo que a su vez hace que ParseInt y otros m茅todos de conversi贸n similares queden obsoletos.
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.