Random Javascript variable not works

I am using the following code, it is suppose to put a random value for firstVId but it is not working why?

<script src="http://www.youtube.com/player_api"></script>





    <script>

Math.randomB = function(min, max) {

    return Math.floor(Math.random() * (max - min + 1)) + min;

}



var array = [4L5g2kN9oUU, NiyUYuO-n38, cxYFNrVc81I];

var firstVId = array[Math.randomB(0, array.length - 1)];

      

var secondVId = '6Z1y3b46A1k';



var bPlayedSecondTime = false;





// create youtube player

var player;

function onYouTubePlayerAPIReady() {

    player = new YT.Player('player', {

      height: '390',

      width: '640',

      videoId: firstVId,

      events: {

        'onReady': onPlayerReady,

        'onStateChange': onPlayerStateChange

      }

    });

}



// autoplay video

function onPlayerReady(event) {

    event.target.playVideo();

}



// when video ends

function onPlayerStateChange(event) {        

    if(event.data === 0) {          

      



        if(bPlayedSecondTime == false)

        {

          player.loadVideoById(secondVId);

          bPlayedSecondTime = true;

          event.target.playVideo();



        }

    }

}



    </script>

The problem is that these string are missing quotes. It should be

var array = ['4L5g2kN9oUU', 'NiyUYuO-n38', 'cxYFNrVc81I'];

The quotes are indicating that these are values that are simply text and do not have any special meaning in javascript.

For more information on strings, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.