Home > Enterprise >  Return Array After For Loop
Return Array After For Loop

Time:01-01

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!

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

function writeCards(names, event) {

 // 1) Create a new, empty array to hold the messages.

 // So, this should be done within the function. We want to be
 // able to update this locally-scoped array with the information
 // we get from each name in the array, combined with the event
 const messages = [];

  for (let i = 0; i < names.length; i  ) {

    // 2) using string interpolation

    // What's great is that you've already discovered template strings
    // String concatenation has its uses but this far better.
    const message = `Thank you, ${names[i]}, for the wonderful ${event} gift!`;

    // So, instead of logging the message to the console
    // we're going to push it to the messages array
    messages.push(message);
  }

  // 3) After the loop finishes and all of the messages
  // have been added to the new array, return the new array.
  return messages;

}

console.log(writeCards(['Bob', 'Sue'], 'wedding'));

CodePudding user response:

I would rather suggest to go with @DoneDeal0 answer's.

In case, if you would like to go with for loop, there's one more cleaner way to go with it. You can use forEach() function given by javascript, as it takes two parameters in it, first is the value of each index and second is the index number itself. Note: forEach() is just similar to for(i=0; i<n; i ).

names = ["Robert", "King", "Joey"];

function writeCards(names) {
  let messages = [];
  names.forEach((name, index) => {
    messages.push("Thank you, "   name   " at "   index   " for the wonderful gift");
  });
  return messages;
}

console.log(writeCards(names));
  • Related