My Coding Quiz #13

avatar

My Coding Quiz #13 👨‍💻🛠️🧩

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


Mi Quiz de Programación #13 👨‍💻🛠️🧩

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



0
0
0.000
12 comments
avatar
avatar

Congratulations @eniolw! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Women's World Cup Contest - Quarter Finals - Recap of Day 2
Women's World Cup Contest - Quarter Finals - Recap of Day 1
LEO Power Up Day - August 15, 2023
0
0
0.000
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
avatar

Saludos. La respuesta es SS ya que los corchetes [::-1] que van después del strip(), recorren los valores por defecto de 0 al inicio y el último indice del string, grabando esta en inversa en b y dándonos "ecneicS".

Entonces, si sumamos el índice 1 de " Science " (Que va después del espacio en el índice 0), que será S, con el último indice de B, que también será S, tendremos SS.

Excelente Test.

0
0
0.000
avatar

Muy bien, esa es la respuesta y la razón.

0
0
0.000