DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Edited on

1

Paracetamol.js💊| #217: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Intermedio

const fn = (arr) => {
  let x = new Set(arr)
  x.add(1).add(5)
  x.delete(1)
  return x.size
}

console.log(fn([1,2,4,1,2])) // 🤔
Enter fullscreen mode Exit fullscreen mode
  • A. 3
  • B. 7
  • C. 6
  • D. 5

Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:

  • A. 3

Set es una estructura de datos en JavaScript que solo acepta valores únicos.
La variable x almacena nuestro Set, el método add permite agregar nuevos valores al Set, x.add(1).add(5) agrega solo el valor 5 porque el valor 1 ya existe en el Set; posteriormente eliminamos el valor 1 con la línea x.delete(1), finalmente regresamos la longitud del Set que es 3.

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay