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} .
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); } } }