Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save teffcode/105c7a8d0db2055f363695b26a4fdff7 to your computer and use it in GitHub Desktop.
Save teffcode/105c7a8d0db2055f363695b26a4fdff7 to your computer and use it in GitHub Desktop.

👋🏼 Welcome 👋🏼

Quiz banner

Instagram | Twitter | LinkedIn


Choose your language 👅



English version 🚀


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()


Spanish version 🚀


¿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()


@raulmar0
Copy link

raulmar0 commented Feb 1, 2021

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!

@leonelrr12
Copy link

A

@xsrpm
Copy link

xsrpm commented Oct 22, 2021

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!

Esta forma es menos marciana. Me hizo recordar a los problemas de matemática forzados a ser complejos por una jugada.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment