迎接,所以我一直试图让我的葡萄藤发生器每次运行时产生一个随机的葡萄藤。我试图让它最后3个给定的葡萄藤永远不会重复。
请注意,我只想检查最后3个给定的葡萄藤以避免。
var vines = [
['https://www.youtube.com/watch?v=21yI4dtAyJQ', 'Gotta love dogs'],
['https://www.youtube.com/watch?v=MB1Ud95U8FE', 'The struggle of measuring something without measuring tape'],
['https://www.youtube.com/watch?v=b6l63-AdU6Y', 'When the cars full but u still tryna roll with the squad'],
['https://www.youtube.com/watch?v=19KOD8-mGm4', 'When the pool water is too cold..'],
['https://www.youtube.com/watch?v=05Bf5vs8j9Q', 'Can I please get a waffle?'],
['https://www.youtube.com/watch?v=2IekMo_DQmw', 'Cheese of truth']
];
var random = Math.floor((Math.random() * vines.length) + 0);
var prev = [];
function Scramble(number, last) {
if(last.includes(number)) {
Scramble(random, prev); // If the previous number matches current rerun function for a new number.
}
if(last.length === 3) {
last.pop(); // Removes the 3rd vine to make room for a new one.
}
last.unshift(number); // Register the last given number.
console.log(`[${number+1}] ${vines[number][1]}\n${vines[number][0]}`)
}
Scramble(random, prev);
答案 0 :(得分:1)
调用Scramble(random, prev);
函数时需要更新随机数。在调用Scramble(random, prev);
之后,您需要从函数返回。
请检查代码
var vines = [
['https://www.youtube.com/watch?v=21yI4dtAyJQ', 'Gotta love dogs'],
['https://www.youtube.com/watch?v=MB1Ud95U8FE', 'The struggle of measuring something without measuring tape'],
['https://www.youtube.com/watch?v=b6l63-AdU6Y', 'When the cars full but u still tryna roll with the squad'],
['https://www.youtube.com/watch?v=19KOD8-mGm4', 'When the pool water is too cold..'],
['https://www.youtube.com/watch?v=05Bf5vs8j9Q', 'Can I please get a waffle?'],
['https://www.youtube.com/watch?v=2IekMo_DQmw', 'Cheese of truth']
];
var random = Math.floor((Math.random() * vines.length) + 0);
var prev = [];
function Scramble(number, last) {
// console.log(number);
if(last.includes(number)) {
random = Math.floor((Math.random() * vines.length) + 0);
Scramble(random, prev);
return;
}
if(last.length === 3) {
last.pop(); // Removes the 3rd vine to make room for a new one.
}
last.unshift(number); // Register the last given number.
console.log(`[${number+1}] ${vines[number][1]}\n${vines[number][0]}`)
}
Scramble(random, prev);