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

92
Views
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 answers
Answer question

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 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!