My Coding Quiz #36

in #hive-169321last year

My Coding Quiz #36 👨‍💻🛠️🧩

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: false. This script is similar to how curation works in blogging sites like Hive.

We see that three constant variables vote, str and comment are created. The variable str is an auxiliary that contains a string data. It is used shortly after to create the comment variable. However, when we try to parse its content we discover that the value of str does not follow the JSON format. The problem is that it uses single quotes ('') instead of double quotes (""). Did you notice this? If you didn't, you must have been suspicious of the fact that a try-catch clause was used. As a consequence comment is undefined.

Then we create the flags object with the upvoted and commented fields. We see that we use the negation operator twice (!!), which helps to get the truth value of vote and comment, whether truthy or falsy. We do this because we are only interested in the boolean value, not the complete data.

Finally, we create the constant variable curated using the expression Object.values(flags).every(v => !!v). The static method Object.values() allows you to create an array from the values of a given object. This is incredibly useful if you want to iterate over the contents of an object using for or map, etc. In our script the array created is [ true, false ] because comment is undefined and this is a falsy value.

Then the every method receives the iterable implicitly and the callback function v => !!v. The every method checks if all the elements of the array satisfy the given condition. In our case, the array [ true, false ] does not, because the second item is false. As a consequence every returns false and is assigned to curated. The output is therefore false, indicating that the curation has not been completed.


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


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

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: false. Este script es similar a cómo funciona la curación en sitios de blogs como Hive.

Vemos que se crean tres variables constantes vote, str y comment. La variable str es un auxiliar que contiene una cadena de datos. Se utiliza poco después para crear la variable comment. Sin embargo, cuando intentamos analizar su contenido descubrimos que el valor de str no sigue el formato JSON. El problema es que utiliza comillas simples ('') en lugar de comillas dobles (""). ¿Notaste esto? Si no lo hiciste, debiste haber sospechado del hecho de que se utilizó una cláusula try-catch. Como consecuencia, comment es undefined.

Luego creamos el objeto flags con los campos upvoted y commented. Vemos que usamos el operador negación dos veces (!!), que ayuda a obtener el valor de verdad de vote y comment, ya sea truthy o falsy. Hacemos esto porque solo nos interesa el valor booleano, no los datos completos.

Finalmente, creamos la variable constante curated usando la expresión Object.values(flags).every(v => !!v). El método estático Object.values() permite crear un arreglo a partir de los valores de un objeto determinado. Esto es increíblemente útil si deseas iterar sobre el contenido de un objeto usando for o map, etc. En nuestro script, el arreglo creado es [ true, false ] porque comentario es undefined y este es un valor falsy.

Entonces el método every recibe el iterable implícitamente y la función callback v => !!v. El método every comprueba si todos los elementos del arreglo satisfacen la condición dada. En nuestro caso, el arreglo [ true, false ] no es así, porque el segundo elemento es falso. Como consecuencia, every devuelve false y se asigna a curated. Por lo tanto, el resultado es false, lo que indica que la curación no se ha completado.


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.

Sort:  

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.