0

Greets, so I've been trying to make my vine-generator to generate a random vine each time you run it. I am trying to make it so that the last 3 given vines will never repeat themselves.

Note that I only want to check for the last 3 given vines to avoid.

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);
versvs
  • 643
  • 2
  • 12
  • 30
Unkn0wn
  • 70
  • 8
  • 1
    Great! So what's your *question*? – Obsidian Age Apr 04 '18 at 04:34
  • "great, this could take forever" - Jim Carey It always makes me roll in my grave when people use iteration or recursion to avoid certain random numbers -- just create a random number from a smaller range. If I said to you, `var randomDiceRoll = function () { var roll = Math.floor(Math.random() * 10000); return (roll < 6) ? roll + 1 : randomDiceRoll(); } ` what would you say? – jcarpenter2 Apr 04 '18 at 04:39
  • @ObsidianAge well, since the prev array dosnt seem to want to store any values I give it, it dosnt check if the given number have been given before within the last 3 vines. So even thought I am trying to prevent that, it ignores that if statement completly. So I'd say that my question is, 'How would I prevent the code from not doing that'. – Unkn0wn Apr 04 '18 at 04:52
  • @jcarpenter2 that I can't understand your code. I understand the joke that your trying to make. However as you may have noticed I am no experienced javascripter, what I know I've learned myself. But what you said regarding creating a number from a smaller range. It's already limited to the number of vines, however yes tecniqually I only search for vines.length-3. But that woudnt help me knowing what vines these 3 vines are. I might get your initial point, but you asked me what i'd say about that. – Unkn0wn Apr 04 '18 at 04:57
  • You don't want random numbers. You want to shuffle an existing set of numbers. – Oliver Apr 04 '18 at 05:31
  • Possible duplicate of [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Oliver Apr 04 '18 at 05:31

1 Answers1

1

You need update random number when calling Scramble(random, prev); function. After calling Scramble(random, prev); you need to return from the function.

Please check code

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);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33