Instagram | Twitter | LinkedIn
What is the output of the following code: A, B or C?
Click here to see the correct answer and explanation 👀
| Correct Answer | Explanation |
|---|---|
| A | The filter() method creates a new array with all elements that pass the test implemented by the provided function (index < 8). The call() method essentially forces the value in this in whichever function .call is applied on. In this case, we invoke the (element, index) => index < 8 function and .call() forces the this of that function to be the date string. |
Explanation based on 👉🏼 Array.prototype.filter() & JavaScript .call() .apply() and .bind() — explained to a total noob | Medium
Code:
function birthday() {
var date = "February 1, 2021"
var month = [].filter.call(date, (element, index) => index < 8)
console.log(month)
}
birthday()
¿Qué imprime el siguiente código: A, B o C?
Haz click aquí para ver la respuesta correcta y su explicación 👀
| Respuesta correcta | Explicación |
|---|---|
| A | El método filter() crea un nuevo array con todos los elementos que cumplan la condición implementada por la función dada (index < 8). El método call() esencialmente fuerza el valor de this en cualquier función a la que se aplique .call(). En este caso, invocamos la función (element, index) => index < 8 y .call() hace que el this de esta función sea el string date. |
Explicación basada en 👉🏼 Array.prototype.filter() | MDN & JavaScript .call() .apply() and .bind() — explained to a total noob | Medium ]()
Código:
function birthday() {
var date = "February 1, 2021"
var month = [].filter.call(date, (element, index) => index < 8)
console.log(month)
}
birthday()


También podemos filtrar el string haciendo uso del spread operator
var month = [...date].filter((element, index) => index < 8)Muchas gracias por tus quizes Teff!