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

105
Views
do-while loop still running even thought the conditional is false

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [];
let index = Math.floor(Math.random() * arr1.length);

for(let i = 0; i < arr1.length; i++) {
    arr2.push(arr1[index]);
    do {
        index = Math.floor(Math.random() * arr1.length);
        console.log(arr2.includes(arr1[index])); //thís is to see the result
    } while (arr2.includes(arr1[index]))
}

console.log('arr2: ', arr2);

I want to make the new unordered array from the first array. But when I run this code, the loop becomes an infinite loop although the conditional is false. My question is the reason make the loop still running with the false conditional?

enter image description here

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