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: 319. Line 1 declares a variable x and assigns it the value 321. Then a variable y is declared and it gets its value from a string created with the value of x, meaning, '321'. The built-in method toString takes care of this. Then, a z variable is declared and it gets the value null.
Now, the most interesting part happens on line 4. Note the presence of the built-in method slice, which allows you to extract a subarray or substring from another array or string. . We see that it is used twice; let's take a closer look.
Evaluating the expression on line 4 from left to right, we first solve the nested operation y.length/2+1 which produces 2.5. Both 0 and 2.5 are the indexes for slice, but the function only takes the integer part of the floating number. Therefore, a slice is made from index 0 inclusive to index 2 exclusive, resulting in the value '32'.
This value is sliced again as dictated by the final part of the expression, but this time we have a single argument specified and it is negative. When this occurs, it is assumed that values will be extracted from the end of the array or string. In this case, the argument -1 indicates that only the last element of the array will be extracted, resulting in the value '2'. Of course, the whole expression could have been better written, but the point is that you can test your knowledge of these details.
Finally we get the subtraction y - z which would be '321' - '2'. Javascript in this case looks for the most convenient way to apply the - operator and does so by converting the operands to numeric values. We have that 321 - 2 is 319 and that is the output.
A shout out to @splash-of-angs63 who solved this quiz!
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, Hive Learners 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: 319. La l铆nea 1 declara una variable x y le asigna el valor 321. Luego se declara una variable y a partir de la cadena de caracteres creada con el valor de x y este valor ser铆a '321'. El m茅todo incorporado toString se encarga de esto. Luego se declara una variable z y se le asigna el valor null.
Ahora bien, la parte m谩s interesante ocurre en la l铆nea 4. Notamos la presencia del m茅todo incorporado slice, el cual permite extraer un subarreglo o una subcadena a partir de otro arreglo o cadena. Vemos que se usa dos veces; echemos un vistazo.
Evaluando la expresi贸n en la l铆nea 4 de izquierda a derecha, resolvemos primero la operaci贸n anidada y.length/2+1 que produce 2.5. Tanto 0 como 2.5 son los 铆ndices para slice, pero la funci贸n solo toma la parte entera del flotante. Se hace entonces un rebanado desde el 铆ndice 0 inclusivo hasta el 铆ndice 2 exclusivo, resultando en el valor '32'.
A este valor se le aplica un nuevo rebanado seg煤n dicta la parte final de la expresi贸n, pero esta vez tenemos un 煤nico argumento especificado y es negativo. Cuando esto ocurre, se asume que se extraer谩n valores desde el final del arreglo o cadena. En este caso, el argumento -1 indica que solo se extraer谩 el 煤ltimo elemento del arreglo, resultando en el valor '2'. Por supuesto, toda la expresi贸n pudo haber sido mejor escrita, pero la idea es que compruebes tus conocimientos de estos detalles.
Finalmente se obtiene la resta y - z que ser铆a '321' - '2'. Javascript en este caso busca la manera m谩s conveniente de aplicar el operador - y lo logra convirtiendo los operandos a valores num茅ricos. Tenemos que 321 - 2 es 319 y esa es la salida.
Qu茅 bien por @splash-of-angs63 quien resolvi贸 este quiz.
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, Hive Learners y otras.