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

312
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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