Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
Necesita ayuda para modificar JS for loop

Editado: Nuevo en la codificación. Estoy copiando y ajustando el código de un proyecto de JavaScript simple, y aquí está el bucle for que necesito modificar:

 randomize(ShipClass = Ship) { this.removeAllShips(); for (let size = 5; size >=2; size--) { for (let n = 0; n < 6 - size; n++) { const direction = getRandomFrom("row", "column"); const ship = new ShipClass(size, direction); while (!ship.placed) { const x = getRandomBetween(0, 9); const y = getRandomBetween(0, 9); this.removeShip(ship); this.addShip(ship, x, y); } }

Coloca aleatoriamente 10 barcos con los siguientes tamaños: {2,2,2,2,3,3,3,4,4,5}.

Necesito modificarlo para que coloque solo 5 naves con los siguientes tamaños: {2,3,3,4,5} .

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simplemente recorra la matriz de tamaños deseados con un bucle for..of :

 randomize(ShipClass = Ship) { this.removeAllShips(); for (let size of [2, 3, 3, 4, 5]) { const direction = getRandomFrom("row", "column"); const ship = new ShipClass(size, direction); while (!ship.placed) { const x = getRandomBetween(0, 9); const y = getRandomBetween(0, 9); this.removeShip(ship); this.addShip(ship, x, y); } } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!