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

130
Views
How do i prevent math.random form selecting the same value twice

var navne = ["peter", "mads", "Per"]

var randomNavn = navne[Math.floor(Math.random() * navne.length)];

var spørgsmål = [`bund din drink ${randomNavn}`, `tag 2 shots ${randomNavn}`, `del 2 tåre ud ${randomNavn}`]


var randomItem = spørgsmål[Math.floor(Math.random() * spørgsmål.length)];


function næsteSpørgsmål() {

  if (!spørgsmål.length) {
    for (var i = 0; i < 50; i++) {
      spørgsmål.push(i);
    }

    var val = spørgsmål[index]

    randomItem.splice(randomItem, 1);

    console.log(randomItem)
    return val;
  }
}

console.log(næsteSpørgsmål());

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

0

the best way to achieve this, is to remove the selected element from the array.

// generate random index
let randomIndex = Math.floor(Math.random() * spørgsmål.length)
// get item at the generated index
let randomItem = spørgsmål[randomIndex]
// remove item from array, so it can't be selected again
spørgsmål.splice(randomIndex, 1)
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!