Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda