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

152
Visualizações
How to fill String/Array with character recursion

i have a little issue in JS-Code and I would appreciate your help So i have an Array like this:

let topMap= [
'................**********........................',
'...............*..........*.......................',
'..........*****............*........*.............',
'.........*.................*.......*.*....*****...',
]

Now i can fill a Line in this map with character '*'. I split this Array in to a String, set a character at position and transfer back in to Array.

const getCharFromArrayPosition = (x, y) => topMap[x][y]

const replaceChar = (x,y) => { 
topMap= topMap.map(x => x.split(''))
topMap[x][y] = char
topMap = topMap.map(x => x.join(''))
}

const floodFill = (x,y) => {

    if(getCharFromArrayPosition(x,y) !== char){
        replaceChar(x,y)
        floodFill(x,y+1)
    }
    return topMap 
}

So if i console.log(floodFill(0,0) i get this

  '**************************........................',
  '...............*..........*.......................',
  '..........*****............*........*.............',
  '.........*.................*.......*.*....*****...',

I am filling the y-position and it is okey but, how can i fill the x-position too with recursion? Or in other words how to fill from one character to other ? thank you

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Right now, your recursion is only going in a single direction - to the right.

floodFill(x, y + 1)

Recurse in all directions instead. You'll also need to keep track of indicies visited to avoid endless recursion. An array of arrays may be easier to work with than an array of strings, too.

const topMap = [
  '................**********........................',
  '...............*..........*.......................',
  '..........*****............*........*.............',
  '.........*.................*.......*.*....*****...',
].map(str => [...str]);

const visited = new Set();
const floodFill = (y, x) => {
  const key = y + '_' + x;
  if (visited.has(key) || topMap[y]?.[x] === undefined || topMap[y][x] !== '.') {
    return;
  }
  visited.add(key);
  topMap[y][x] = '*';
  floodFill(y, x + 1);
  floodFill(y, x - 1);
  floodFill(y + 1, x);
  floodFill(y - 1, x);
}
floodFill(0, 0);
console.log(topMap.map(subarr => subarr.join('')));

about 4 years ago · Santiago Gelvez 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