Skip to content
Advertisement

How to print items within an array sequentially

I’m learning Node and I’m attempting to create a readme generator. I’ve created my prompts to confirm if the user would like to add gifs/images to their markdown. I’ve made it so that if the user confirmed then they can enter in the number and then I pass it into a for loop that generators the markdown text for adding an image. What I’m trying to do is to get it to print list style. So far I’ve gotten it so that it’ll print but only as:

![Alt Text](Link or File Path),![Alt Text](Link or File Path),![Alt Text](Link or File Path)

When I’d rather it printed as

![Alt Text](Link or File Path)
![Alt Text](Link or File Path)
![Alt Text](Link or File Path)

My gut tells me to try using forEach but I’m not entirely sure what I should put into my callback function. Any help is appreciated.

const generateUsageMedia = features => {
  if (features.mediaConfirm) {
    const mediaItems = [];
    for (let i = 0; i <= Number(features.mediaCount); i++) {
      mediaItems.push(`![Alt Text](Link or File Path)`);
    }
    return [...mediaItems];
  } else {
    return;
  }
}

Advertisement

Answer

You can join the array on the new line character (n).

return mediaItems.join('n');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement