Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
Need help to modify JS for loop

Edited: New to coding. I’m copying and adjusting code from a simple JavaScript project, and here is the for loop I need to modify:

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

It randomly places 10 ships with the following sizes: {2,2,2,2,3,3,3,4,4,5}.

I need to modify it so that it places only 5 ships with the following sizes: {2,3,3,4,5}.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just loop over your desired sizes array with a for..of loop:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda