Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
Cree una matriz a partir de otros elementos de matrices aleatorias en un bucle for en Javascrypt

Si queremos construir una frase usando palabras aleatorias de arreglos, podríamos usar

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

Pero como ves arriba, esto tomará demasiado tiempo para varias palabras, así que en lugar de esa parte gorda traté de construir la frase con un bucle for. El problema aquí es que en el bucle for, part_i se lee como texto sin formato, y no como part_number(i)

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

¿Alguna idea para solucionar esto? Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo que necesita aquí es una matriz de esas matrices:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!