DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Edited on

1

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

Explica este código JavaScript

Dificultad: Básico

const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6).map((x) => x ** 2)); 🤔
Enter fullscreen mode Exit fullscreen mode
  • A. [1, 4, 36]
  • B. [ 1, 4, 36, 16, 25 ]
  • C. [ 1, 36, 9, 16, 25 ]
  • D. undefined

Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando • Edited

Respuesta:

B. [ 1, 4, 36, 16, 25 ]
with es un nuevo método para arreglos en JavaScript que permite inyectar un valor en una posición específica.
Recibe 2 parámetros: with(index, value) donde:

  • index es la posición del arreglo que deseamos obtener
  • value es el valor que queremos inyectar

En el ejemplo tenemos: arr.with(2, 6) lo que significa que pondremos el valor 6 en la posición 2, quedando el siguiente array: [1, 2, 6, 4, 5].

Ahora con el map iteramos elemento por elemento elevando al cuadrado cada número (esto con el operador **) siendo el resultado final: [ 1, 4, 36, 16, 25 ]

Image of Bright Data

Ensure Data Quality Across Sources – Manage and normalize data effortlessly.

Maintain high-quality, consistent data across multiple sources with our efficient data management tools.

Manage Data

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay