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

89
Vistas
Build array from other random arrays elements in a for loop in Javascrypt

If we want to build a phrase using random words of arrays, we could use

part_0=["Hello",....]
part_1=["I am",.....]

phrase=part_0[Math.round(Math.random() *(part_0.length-1))]+part_1[Math.round(Math.random() *
(part_1.length-1))]+...+part_n[Math.round(Math.random() *(part_n.length-1))]

But as you se above this will take too long for a n number of words, so instead of that fat part i tried to build the phrase with a for loop. The problem here is that in the for loop, part_i is read plain text, and not as part_number(i)

for (var _i = 0; _i < 3; _i++) {
  phrase.add(part_i);
};

Any idea to solve this? Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What you need here is an array of those arrays:

function pickRandom(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}

let sections = [
    ["Hello,", "Hi,", "Howdy!", "Hey,"],
    ["I am", "You are", "They are", "We are"],
    ["making", "building", "crafting", "decorating"],
    ["a tower", "a machine", "a table", "a chair"],
    ["with", "using", "gluing", "consisting of"],
    ["toilet paper", "plastic cups", "paper clips", "staples"]
];

let phrase = sections.map(pickRandom).join(" ");

console.log(phrase);

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