2

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;
  }
}
6
  • 1
    return mediaItems.join('\n')? Commented Aug 1, 2020 at 21:49
  • please add the array and the wanted result. Commented Aug 1, 2020 at 21:50
  • The .join('\n') worked. Thanks @hev1 Commented Aug 1, 2020 at 21:54
  • How are you calling generateUsageMedia Also, why are you doing a spread instead of simply returning mediaItems Commented Aug 1, 2020 at 21:54
  • 1
    @SSmith No problem. Commented Aug 1, 2020 at 21:55

1 Answer 1

2

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

return mediaItems.join('\n');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.