我想创建一个以数组作为参数的函数,从1 - 10
创建一个随机数,并运行直到它创建一个不在数组中的数字,并返回它。
这是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);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="test">
Test
</button>
&#13;
答案 0 :(得分:6)
使用.includes
Array方法查看数组中是否存在值:
https://jsfiddle.net/an3ogr34/8/
function contains(arrParam) {
while (true) {
var random = 1 + Math.floor(Math.random() * 10);
if (!arrParam.includes(random)) return random;
}
}
var arr = [1, 2, 3, 4, 5, 6];
$(".test").on("click", function() {
const result = contains(arr);
console.log(result);
});
答案 1 :(得分:0)
多重错误
arr
函数作为参数传递时,{li> contains
被contains
函数使用。 arr
函数无法访问全局true
您同时返回false
和random
,即使arr
中包含contains
项, function contains() {
var check = false;
while (!check) {
var random = Math.floor(Math.random() * 10);
check = !arr.includes(random);
}
return random;
}
从函数返回。
成功
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>
import pandas as pd
d = {'A': ['a1','a1','a2'], 'B': ['b1','b2','b3'], 'C': ['c1','c2','c2'], 'v': [0,5,1]}
df = pd.DataFrame(data=d)
df = df.set_index(['A','B','C'])
for i in set(df.index):
df_sel = df.loc[i].reset_index()
df_sel = df_sel.T #or any other operation on dataframe
df_sel.to_csv(str(i) + '.csv')
答案 2 :(得分:0)
您可以使用do ... while
循环,因为您需要至少一个循环来检查随机数,并检查Array#includes
是否继续循环。
不要忘记移交数组以检查并在函数内部使用该数组。
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));
答案 3 :(得分:0)
我使用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>
答案 4 :(得分:0)
function contains(arr) {
if(arr.length<10){
while(true){
var random = Math.floor(Math.random() * 10);
random += 1;
if(!arr.includes(random)){
return random;
}
}
}
return -1;
}
var arr = [1, 2, 3, 4, 5, 6];
$(".test").on("click", function () {
console.log(contains(arr)); //for print in console
contains(arr);
});