0

I am trying to take an array of strings, and using forEach return a single string containing all the array items. The directions specifically exclude using .join(), which would have been my 1st choice.

    // This is a list of words
let words = ['Loops', 'are', 'a', 'great', 'way', 'to', 'find', 'elements', 'in', 'an', 'array'];

// TODO: implement this function to return a string containing all words in a paragraph.
function createParagraph(words) {
  let paragraph = '';

  return paragraph;
}

// Prints paragraph to console
console.log(createParagraph(words));

// don't change this line
if (typeof module !== 'undefined') {
  module.exports = createParagraph;
}

I have tried:

words.forEach(words.join(' ')).push(paragraph);
5
  • If you are not allowed to use join(), you can loop over the items and each time perform paragraph += " " + currentWord Commented Jun 9, 2022 at 15:57
  • words.forEach(word => paragraph += word += " "); That worked! Thank you so much! Commented Jun 9, 2022 at 16:08
  • @roninroyal_ The directions specifically exclude using .join() - Why ? I am just curious to know the reason. Commented Jun 10, 2022 at 13:21
  • @RohìtJíndal it's just the directions for the assignment. This is part of a class "practice." The funny thing is, when we test we used .join(). Commented Jun 19, 2022 at 14:27
  • @roninroyal_ Yes got it. I added an answer as per the understanding. Hope this will help you. Commented Jun 20, 2022 at 5:28

2 Answers 2

1

An alternative to .join is .reduce

here's a quick implementation:

words.reduce((previous, current) => {
    return previous + " " + current
})
// will return the words with a space seperating them

alternatively, if you want to do this with pure forEach, you can try

function createParagraph(words){
    let paragraph = '';
    words.forEach((el, i) => {
        paragraph += el
        if(i < words.length-1) paragraph += " "
    })
    return paragraph
}
Sign up to request clarification or add additional context in comments.

Comments

0

As per my understanding you want to convert the array into a string without help of JavaScript inbuilt methods. If Yes, Then you are good to go with the Array.forEach method for the iteration of an array and concatenate the elements by using arithmetic symbol.

Live Demo :

// This is a list of words
let words = ['Loops', 'are', 'a', 'great', 'way', 'to', 'find', 'elements', 'in', 'an', 'array'];

// Function which returns a string containing all words in a paragraph.
function createParagraph(words) {
  let paragraph = '';
  words.forEach(word => {
    paragraph += (words[words.length - 1] !== word) ? word += " " : word
  });
  return paragraph;
}

// Prints paragraph to console
console.log(createParagraph(words));

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.