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>
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>
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));
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>
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#...
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 ...
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 ...