Episode Generator

Sometimes, I like to watch random episodes of my favorite shows instead of watching them in order. In this article I will use Friends on Netflix as an example. I used to do this manually by looking up a list of episodes to see how many there are (236 for Friends), then I would generate a random number between 1-236 and finally I would look up the episode on Netflix.

My idea for this post is to use JavaScript to do all three steps automatically. What it does is each time you refresh, it will generate a new random number and you can click on the link to view it directly on Netflix. So refresh and try it out.

Refresh for a Random Friends Episode Link

Loading...

Each time you refresh you will see that the episode number will change (a number between 1 and 236) and the Netflix link will be updated correspondingly.

To add this to your own website, add a div to your website:


          <div id="randomDiv" class="starter-template">
            <h1>Your Random Friends Episode</h1>
            <p>Loading...</p>
          </div>
        

Then add the following JavaScript code at the end of your body:


        function generateRandomEpisode() {
          //htmlContent is the html code that will eventually contain our link
          var htmlContent = '<h2>Refresh for a Random Friends Episode Link</h2>';
          //number of episodes in your show (236 for Friends)
          var numberOfEpisodes = 236;
          //generates a random number between 0 and 235 inclusive of bounds.
          var randomOffset = Math.floor(Math.random()*numberOfEpisodes);
          //Netflix shows always start with this URL and then an identifier
          var baseURL = "https://www.netflix.com/watch/"
          //Find out the netflix ID for episode 1 of season 1,
          //which is the number right after the base URL.
          var baseNetflixID = 70273997;
          //Netflix uses consecutive IDs for their series (very useful :D).
          //So we just add our random offset to our base ID.
          var generatedEpisodeID = baseNetflixID + randomOffset;
          //Complete our generated URL:
          baseURL += generatedEpisodeID.toString();
          //Append our URL and episode number to the htmlContent.
          htmlContent += '<p> <a href="'+baseURL+'">Click here to watch your randomly
              generated Friends episode on Netflix</a>. It is episode number: ' +
              (randomOffset + 1).toString() + '.</p>';
          //Finally we actually show our htmlContent to the website.
          document.getElementById("randomDiv").innerHTML = htmlContent;
        }
        generateRandomEpisode();
      

You can change this code to suite your own show by assigning the numberOfEpisodes variable the number of episodes in your show and by setting the baseNetflixID to that of the episode one season one of your show. This is assuming your show is on Netflix of course.

Refresh for a Random Rick and Morty Link

Loading...