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

309
Vistas
Return Array After For Loop

Having a hard time understanding for loops in arrays. Trying to create a Thank You card creator and these are the steps I'm trying to follow:

  1. Create a new, empty array to hold the messages
  2. Iterate through the input array and inside the loop build out the 'thank you' message for each name using string interpolation, then add that message to the new array you created
  3. After the loop finishes and all of the messages have been added to the new array, return the new array.

 const names = []
function writeCards(names, event) {
  for (let i = 0; i < names.length; i++) {
    console.log(`Thank you, ${names[i]} for the wonderful ${event} gift!`);
  return names;
}

Not sure if I'm on the right track. Thanks for your help!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use Array.push()

function writeCards(names, event) {
  let messages = []
  for (let i = 0; i < names.length - 1; i++) {
    messages.push("Thank you, " + names[i] + " for the wonderful " + event + " gift!")
  }
  return messages;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I know your question is focused on for loop, but just in case, you might be interested in using map to achieve the desired result in a more concise manner:

const names = ["Joe", "Nina"]

function writeCards(names, event) {
   return  names.map(name=> `Thank you, ${name} for the wonderful ${event} gift!`)
}

console.log(writeCards(names, "birthday"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

well the i had this issue

i had an empty array outside a loop and wanted to update it inside a loop

and finally return an array with alot of indexes

    //ie 
    //let arr = [] 
    //LOOP then runs
    //then console.log(arr) //console shows [1,2,3,4,5,6,7]

this is how you do it

    let arr = []
    let realArray = [1,2,3,4,5] 
    for (let index = 0; index < realArray.length; index++) { 
       const element = realArray[index] 
       arr.push(element)
    }

    console.log(arr)//your arr = 1,2,3,4,5

this is my first answer here

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