0

I need to create a array with unique values. In here if the created value is include in that array, then need to create another value and again need to check that newly created value exists in that array, if again exists then need to do the same check.

Here is the code I have tried, if I execute this, I think infinite looping scene happening.

let arr = [];
for(let i=0; i<10;i++) {
  let k = 1;
  let pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';
  while(k > 0){
    k++;
    if(arr.indexOf(pass) > -1) {
      pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';
    } else {
      arr.push(pass);
      break;         
    }
   
    console.log(arr)
  }
}

What was the mistake in this code?

2
  • 1
    First of all, (Math.floor(Math.random() * (10 - 6 + 1)) + 6) will get you a number from 6 to 10 if I am not mistaken, second of all how would you want 10 unique items in an array if you generate on 5 unique ? Third of all, the problem is here k > 0 it goes in an infinite loop. Commented Jan 20, 2021 at 15:46
  • Your third point is half correct. The only reason k>0 goes into an infinite loop is because it never hits the break. Commented Jan 20, 2021 at 15:49

2 Answers 2

2

Yep, you're correct. It's an infinite loop.

The problem is the line pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';. This will only ever generate one of 5 values. pass will only ever be

  • 6a
  • 7a
  • 8a
  • 9a
  • 10a

Beause your array is 10 elements long, but you're only filling it with 5 possible elements, you will never be able to fill it with all unique elements. So it will go into an infinite loop trying to generate unique elements but never finding unique elements.

You need to rewrite the calculation of pass to generate more than 5 unique elements. Try pass = (Math.floor(Math.random() * 10))+'a'; and go from there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, My bad :D I got this number generating part for this example, and forgot to think about it's results
0

let arr = [(Math.floor(Math.random() * (10)))+'a'];
 for(let i=0; i<=10;i++) {
   let k = 0;
    let pass = (Math.floor(Math.random() * (10)))+'a';
   while(k < arr.length){
       k++;
        if(arr.indexOf(pass) > -1){
            pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';
        }else {
            arr.push(pass);
            break;
         }
        
  
     }


    }
    
    
    console.log(arr)

variable k is always > 0 in your condition and it loops infinitely.

edit 1:

answer updated based on @Mathew answer

1 Comment

This is half correct. It would drop out of the loop if it ever hit the break. The root of the problem is that it doesn't consistently hit the break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.