Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!