Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
forEach to Join Array Items in JS

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);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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
}
about 4 years ago · Juan Pablo Isaza Denunciar

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda