JavaScript Only Return random number which is not in array

I want to make a function that takes an array as an argument, creates a random number from 1 - 10, and runs until it creates a number that is not in the array, and returns it.

Here is jsfiddle

function contains() {
  var check = false;
  while (!check) {
    var random = Math.floor(Math.random() * 10);
    random += 1;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] === random) {
        console.log(random);
        check = false;
        return false;
      }
    }
    check = true;
    console.log(random);
    return true;
  }
}

var arr = [1, 2, 3, 4, 5, 6];

$(".test").on("click", function() {
  contains(arr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="test">
Test
</button>
Answer:1

Multiple mistakes

  • arr that is being used by contains function is localized when you passed the same as an argument. contains function is not able to access the global arr

  • You are returning both true and false, so even if the random item is included in the arr, contains return from the function.

Make it

function contains() {
  var check = false;
  while (!check) {
    var random = Math.floor(Math.random() * 10);
    check = !arr.includes(random);
  }
  return random;
}

Demo

function contains() {
  var check = false;
  while (!check) {
    var random = Math.floor(Math.random() * 10);
    check = !arr.includes(random);
  }
  return random;
}

var arr = [1, 2, 3, 4, 5, 6];

$(".test").on("click", function() {
  console.log(contains());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button class="test">
Test
</button>
Answer:2

You could use a do ... while loop, because you need at least one loop for checking the random number, and check with Array#includes for continuing the loop.

Don't forget to hand over the array for checking and use that array inside of the function.

function contains(array) {
    var random;
    
    do {
        random = Math.floor(Math.random() * 10) + 1;
    } while (array.includes(random))

    return random;
}

var numbers = [1, 2, 3, 4, 5, 6];

console.log(contains(numbers));
Answer:3

I have proposed the solution with the use of indexOf()

var arr = [1, 2, 3, 4, 5, 6];

function contains() {
  var randomNumber = Math.floor((Math.random() * 10) + 1);
  if (arr.indexOf(randomNumber) === -1) {
    return randomNumber;
  } else {
    console.log(`Number exists: ${randomNumber}`);
  }
}

$(".test").on("click", function() {
  var returnNumber = contains();
  if (!!returnNumber) {
    console.log(`Number not exists: ${returnNumber}`);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="test">
Test
</button>
Answer:4

i am trying to submit a form via Jquery when change event occurs in select field like this: $('select#slc_level').change(function(event){ event.preventDefault(); console.log('changed') $('form#...

i am trying to submit a form via Jquery when change event occurs in select field like this: $('select#slc_level').change(function(event){ event.preventDefault(); console.log('changed') $('form#...

  1. submit form jquery ajax
  2. submit form jquery post
  3. submit form jquery button click
  4. submit form jquery validation
  5. submit form jquery not working
  6. submit form jquery php
  7. submit form jquery preventdefault
  8. submit form jquery with parameters
  9. submit form jquery without ajax
  10. submit form jquery on click
  11. submit form jquery laravel
  12. submit form jquery without refreshing
  13. submit form jquery stack overflow
  14. submit form jquery ajax asp.net
  15. submit form jquery get response
  16. submit form jquery
  17. submit form jquery ajax php mysql
  18. submit form jquery mvc
  19. submit form jquery asp.net mvc
  20. submit form jquery ajax codeigniter

I would like to repeat a text for 2 seconds in a while loop. How do I break the loop after 2 seconds? This is what I have tried so far but it doesn't work: var repeat = true; setTimeout(function() {...

I would like to repeat a text for 2 seconds in a while loop. How do I break the loop after 2 seconds? This is what I have tried so far but it doesn't work: var repeat = true; setTimeout(function() {...

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

  1. isn't
  2. https://www.instagram.com/
  3. isnetworld
  4. instagram
  5. isn't she lovely
  6. isnt it romantic
  7. isna
  8. isn't it ironic
  9. isner
  10. isnull sql
  11. isn network
  12. isnac 2020
  13. isnt she lovely chords
  14. isns
  15. isnan
  16. isn't it
  17. isnull
  18. isn't it romantic cast
  19. isna excel
  20. disney plus

Hello, everybody, I have this task: I have an array [4,7,3,6,9] and I have to make an array like this: [4,7,3,6,9] [9,4,7,3,6] [6,9,4,7,3] [3,6,9,4,7] [7,3,6,9,4] I have to make a program where ...

Hello, everybody, I have this task: I have an array [4,7,3,6,9] and I have to make an array like this: [4,7,3,6,9] [9,4,7,3,6] [6,9,4,7,3] [3,6,9,4,7] [7,3,6,9,4] I have to make a program where ...

  1. javascript rotate array 90 degrees
  2. javascript rotate array left
  3. javascript rotate array elements
  4. javascript rotate array values
  5. javascript rotate array
  6. javascript rotate array right
  7. js rotate array
  8. js rotate array elements
  9. javascript rotate 2d array
  10. javascript rotate through array
  11. javascript rotate multidimensional array
  12. js rotate 2d array
  13. javascript rotate 2 dimensional array