selnotrab
selnotrab

Reputation: 13

Javascript Random Array

I have to admit I am quite the newbie when it comes to JavaScript. Spent the last few years working on hardware and haven't been able to keep up.

Anyways, I am trying to use Tubular ( http://www.seanmccambridge.com/tubular/ ) except I am trying to pick from an array of video ID's instead of just the one. For example, refreshing the page loads a different video ID and by relation, loads a different video.

This is the actual function:

$().ready(function() {
$('body').tubular('ID-GOES-HERE','wrapper'); 

I have tried doing this on my own but in the end I simply managed to break it entirely. Any suggestions are welcome!

Upvotes: 0

Views: 506

Answers (3)

Humayun
Humayun

Reputation: 1006

If you wanted to get between 1 and 6, you would put

Math.floor(Math.random() * 6) + 1

You can replace 1 and 6 with how much videos you have in that array or use array.length function if you want it to be dynamic.

Try that and see if it works for you.

Upvotes: 0

KingKongFrog
KingKongFrog

Reputation: 14429

var videoIDs = [1,2,3,4,5,6];
var randomID = videoIDs[Math.floor(Math.random() * videoIDs.length)];

$(document).ready(function() {
    $('body').tubular(randomID,'wrapper'); 
});

Upvotes: 0

mattnull
mattnull

Reputation: 307

Something like...

var myIDs = [1,2,3,4,5,6,7,8,9,0];
var selectedID = myIDs[Math.floor(Math.random() * myIDs.length)];

This will give you a randomly selected item from the array

Upvotes: 2

Related Questions