i'd like to use a function that generate two arrays of same length but values of first and second array shouldnt be duplicates and neither values in first array shall not be in the second one.
我想使用一个生成两个相同长度的数组的函数,但第一个和第二个数组的值不应该是重复的,第一个数组中的值都不应该在第二个数组中。
What i've tried so far:
到目前为止我尝试过的事情:
function generateSiruri(width){
var exists = false;
for(var i = 0; i < width; i++){
var randomNumber = Math.floor(Math.random()*101);
var secondRandomNumber = Math.floor(Math.random()*101);
if(sir1[i] == randomNumber && sir2[i] == secondRandomNumber && randomNumber == secondRandomNumber && sir1[i] == secondRandomNumber){
exists = true;
return;
}
if(!exists){
sir1.push(randomNumber);
sir2.push(secondRandomNumber);
}
}
sortAscending(sir1);
sortAscending(sir2);
}
function sortAscending(array){
var sort;
for(i = 0; i < array.length; i++){
for(var j = 0; j < array.length; j++){
if(array[j] > array[j + 1]){
sort = array[j];
array[j] = array[j + 1];
array[j + 1] = sort;
}
}
}
return;
}
Also, if you have a better option to sort these arrays with sortAscending function and a better option of if condition in generateSiruri please suggest. I can't see a better option now. Thx!
此外,如果您有更好的选择,可以使用sortAscending函数对这些数组进行排序,并在generateSiruri中提供if条件的更好选项。我现在看不到更好的选择。谢谢!
2 个解决方案
#1
2
Do this:
做这个:
while(sir1.length < width) {
var randomNumber = Math.floor(Math.random()*101);
if (sir1.indexOf(randomNumber) == -1)
sir1.push(randomNumber);
}
while(sir2.length < width) {
var randomNumber = Math.floor(Math.random()*101);
if ((sir2.indexOf(randomNumber) == -1) && (sir1.indexOf(randomNumber) == -1))
sir2.push(randomNumber);
}
This way, you're first filling sir1 with unique values and then separately filling sir2 with unique values that are also not in sir1.
这样,您首先使用唯一值填充sir1,然后使用也不在sir1中的唯一值分别填充sir2。
#2
2
Given your comment, this smells like homework so I'll break this down more.
鉴于你的评论,这就像家庭作业,所以我会更多地打破这一点。
Start out with a single array and make it twice as long as width
(iterate width*2
times):
从单个数组开始,使其长度为宽度的两倍(迭代宽度* 2倍):
var ary = [];
for (var i = width * 2; i > 0; i--){ /*...*/ }
var ary = []; for(var i = width * 2; i> 0; i - ){/*...*/}
Then you want a number that's not already present in the array (we can do so using Array.prototype.indexOf()
to check):
那么你想要一个数组中尚未存在的数字(我们可以使用Array.prototype.indexOf()来检查):
var r = Math.floor(Math.random() * 101);
while (ary.indexOf(r) != -1) r = Math.floor(Math.random() * 101);
var r = Math.floor(Math.random()* 101); while(ary.indexOf(r)!= -1)r = Math.floor(Math.random()* 101);
Then simple add your unique value to the array
然后简单地将您的唯一值添加到数组中
ary.push(r);
ary.push(R);
Sort it (your bubble sort is fine for all intents and purposes), then you probably want to return it back through your function. Here we return an array of arrays; Using Array.prototype.slice
we can turn the single array into two arrays:
对它进行排序(您的冒泡排序适用于所有意图和目的),然后您可能希望通过您的函数将其返回。这里我们返回一个数组数组;使用Array.prototype.slice,我们可以将单个数组转换为两个数组:
return [ ary.slice(0, width), ary.slice(width) ];
return [ary.slice(0,width),ary.slice(width)];
Given it's not homework...
鉴于它不是家庭作业......
function generateSiruri(width){
var ary = [];
for (var i = width * 2; i > 0; i--){
var r = Math.floor(Math.random() * 101);
while (ary.indexOf(r) != -1) r = Math.floor(Math.random() * 101);
ary.push(r);
}
ary.sort(); // (or your custom sort method)
return [ ary.slice(0, width), ary.slice(width) ];
}
#1
2
Do this:
做这个:
while(sir1.length < width) {
var randomNumber = Math.floor(Math.random()*101);
if (sir1.indexOf(randomNumber) == -1)
sir1.push(randomNumber);
}
while(sir2.length < width) {
var randomNumber = Math.floor(Math.random()*101);
if ((sir2.indexOf(randomNumber) == -1) && (sir1.indexOf(randomNumber) == -1))
sir2.push(randomNumber);
}
This way, you're first filling sir1 with unique values and then separately filling sir2 with unique values that are also not in sir1.
这样,您首先使用唯一值填充sir1,然后使用也不在sir1中的唯一值分别填充sir2。
#2
2
Given your comment, this smells like homework so I'll break this down more.
鉴于你的评论,这就像家庭作业,所以我会更多地打破这一点。
Start out with a single array and make it twice as long as width
(iterate width*2
times):
从单个数组开始,使其长度为宽度的两倍(迭代宽度* 2倍):
var ary = [];
for (var i = width * 2; i > 0; i--){ /*...*/ }
var ary = []; for(var i = width * 2; i> 0; i - ){/*...*/}
Then you want a number that's not already present in the array (we can do so using Array.prototype.indexOf()
to check):
那么你想要一个数组中尚未存在的数字(我们可以使用Array.prototype.indexOf()来检查):
var r = Math.floor(Math.random() * 101);
while (ary.indexOf(r) != -1) r = Math.floor(Math.random() * 101);
var r = Math.floor(Math.random()* 101); while(ary.indexOf(r)!= -1)r = Math.floor(Math.random()* 101);
Then simple add your unique value to the array
然后简单地将您的唯一值添加到数组中
ary.push(r);
ary.push(R);
Sort it (your bubble sort is fine for all intents and purposes), then you probably want to return it back through your function. Here we return an array of arrays; Using Array.prototype.slice
we can turn the single array into two arrays:
对它进行排序(您的冒泡排序适用于所有意图和目的),然后您可能希望通过您的函数将其返回。这里我们返回一个数组数组;使用Array.prototype.slice,我们可以将单个数组转换为两个数组:
return [ ary.slice(0, width), ary.slice(width) ];
return [ary.slice(0,width),ary.slice(width)];
Given it's not homework...
鉴于它不是家庭作业......
function generateSiruri(width){
var ary = [];
for (var i = width * 2; i > 0; i--){
var r = Math.floor(Math.random() * 101);
while (ary.indexOf(r) != -1) r = Math.floor(Math.random() * 101);
ary.push(r);
}
ary.sort(); // (or your custom sort method)
return [ ary.slice(0, width), ary.slice(width) ];
}