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

182
Vistas
For Loop is not redefining what's supposed to be defined

I have this for loop in which I'm trying to replace an index in underlineArr with a character and whenever I try to redefine it so It saves the current variable so I'm able to access it and change it later, nothing is being saved, It keeps using the default array while I'm trying to get the newly edited array.

For Loop:

       const content = collected.content.toLowerCase();
           for (let i = 0; i < underlineArr.length; i++) {
               msg = underlineArr[i] = word[i] == content && underlineArr[i] == "_" ? content : "_";
           }
           msg = underlineArr.join(" ");
           console.log(msg);

This is the underlineArr constant: https://i.stack.imgur.com/vtzj2.png

This is the word constant: "French Toast", Please note the constant is random from an array I created.

Lastly, collected.content is just a string returned.

The output: Output

Expected output:

_ r _ _ _ _ _ _ _ _ _ _
_ r e _ _ _ _ _ _ _ _ _
_ r e n _ _ _ _ _ _ _ _
_ r e n c _ _ _ _ _ _ _
.....
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try to get the index of content character instead of assigning the entire string.

for (let i = 0; i < underlineArr.length; i++) {
   underlineArr[i] = word[i] == content && underlineArr[i] == "_" ? content[i] : "_";
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

What about this... Am I cold or hot on that unclear question?

let word = "French Toast".split("")
let underlineArr = Array(word.length).fill("_")
let result = []
word.forEach((char,i) => {
  let row = [...underlineArr]
  row[i] = word[i]
  result.push(row)
})

multiline = result.map(subarr => subarr.join(" ")).join("\n")

console.log(multiline)

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm unsure of what I did, but I'm pretty sure the previous code I had wouldn't save anything because it'd replace the saved values with _ and in order to fix this,

for (let i = 0; i < underlineArr.length; i++) {
    if (content !== word.toLowerCase()[i]) continue;
    underlineArr[i] = word.toLowerCase()[i];
}
about 4 years ago · Juan Pablo Isaza 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