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: string. First, two constant variables a and b are created whose values, although they represent digits, are actually of type string, since they are literals in quotation marks; nothing surprising.
Line 2 creates a new constant variable c (yes, it sounds strange to talk about "constant variables", but that's what they are). This is the striking part of the quiz that reveals another of Javascript's quirks. Let's take a closer look at what happens on this line:
const c = a +-+- b
We have a series of + and - operators and this does not cause an exception. Rather, it is valid code that concatenates characters and makes conversions as required.
The expression begins to evaluate from left to right. When you say a + ... where a is of type string, what will ultimately happen is a concatenation of strings. So we already know in advance the data type of c.
However, what else is going on there? Well, the -+- b part is something that applies to the second operand entirely, that is, to b. Its value is '7.0'. The last operator - will convert it to the negative number -7, where the floating part is ignored, as it is unnecessary. Then the + operator simply acts as a bridge for the first - operator to change the sign of the number, resulting in 7. This bridge is not optional, as Javascript will generate a syntax error if we write const c = a +-- b.
We then have '9' + '7' which is '97', the value assigned to c.
Line 3 obtains the data type of c using the typeof operator and we already know that the result is 'string'.
At the end of the day, this code might not be very useful, but it is not invalid syntax and would not be suspicious to the interpreter, so we should definitely know how Javascript works. Additionally, it generates observable effects, so I speculate that it could be useful for writing hacks, obfuscations, etc.
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: string. Primero se crean dos variables constantes a y b cuyos valores, aunque representan d铆gitos, son realmente de tipo strings, pues son literales entre comillas; nada sorprendente.
La l铆nea 2 crea una nueva variable constante c (s铆, suena raro hablar de "variables constantes", pero eso es lo que son). Esta es la parte llamativa del quiz que revela otra de las peculiaridades de Javascript. Miremos m谩s de cerca qu茅 ocurre en esta l铆nea:
const c = a +-+- b
Tenemos una serie de operadores + y - y ello no provoca excepci贸n. M谩s bien es c贸digo v谩lido que lo que hace es concatenar caracteres y hacer conversiones seg煤n se requiera.
La expresi贸n comienza a evaluarse de izquierda a derecha. Cuando se dice a + ... donde a es de tipo string, lo que ocurrir谩 ultimadamente es una concatenaci贸n de strings. De modo que ya sabemos de antemano el tipo de dato de c.
Sin embargo, 驴qu茅 m谩s ocurre all铆? Pues bien, la parte -+- b es algo que aplica al segundo operando por completo, es decir, a b. Su valor es '7.0'. El 煤ltimo operador - lo convertir谩 en el n煤mero negativo -7, donde la parte flotante se desprecia, pues es innecesaria. Luego el operador + simplemente hace de puente para que el primer operador - cambie el signo del n煤mero, resultando en 7. Este puente no es opcional, pues Javascript generar谩 un error de sintaxis si escribimos const c = a +-- b.
Tenemos entonces '9' + '7' que es '97', el valor asignado a c.
La l铆nea 3 obtiene el tipo de dato de c usando el operador typeof y ya sabemos que el resultado es 'string'.
Al final del d铆a, este c贸digo podr铆a resultar poco 煤til, pero no es una sintaxis inv谩lida y no ser铆a sospechosa para el int茅rprete, por lo que definitivamente debemos conocer c贸mo funciona Javascript. Adem谩s, genera efectos observables, por lo que especulo con que podr铆a ser 煤til para escribir hacks, ofuscaciones, etc.
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.