Playing YouTube videos from an array

I know how to do it that way already, this is a different way it is able to be done.

I like trying to figure out how to do things that I have not done before.

What you are wanting to do, playing a list of videos at random, seems to be just the thing that you have done before. The youtube iframe API makes that task real easy with the use of playlists and supporting the shuffling of them.

1 Like

I have a question.

Would this have been able to work without this being a function?

function singleYT() {

Was it and is it required?

https://jsfiddle.net/fjhcz837/

 function singleYT() {
        const videos = [
            "0dgNc5S8cLI",
            "mnfmQe8Mv1g",
            "CHahce95B1g",
            "2VwsvrPFr9w"
        ];

        const index = Math.floor(Math.random() * videos.length);
        return videos[index];
    }

    function addPlayer(video) {
        const videoID = singleYT();
        const config = {
            height: 360,
            host: "https://www.youtube-nocookie.com",
            videoId: videoID,

It would certainly be a lot less usable when not a function, but it can work.

How would it be written because I was trying to figure that out.

Replace videos from the return with the array.
Then replace index from the return with the Math line.
Then replace singleYT with all of that return.

It’s butt ugly, but it works.

First this?

return videos[index];

to

return const videos = [ "0dgNc5S8cLI","mnfmQe8Mv1g","CHahce95B1g", "2VwsvrPFr9w"]; [index];

or did I misunderstand?

That’s the array and I replaced the videos inside the return with it.

I’m sorry, I’m confused.

That’s the full assignment that you put in there, which of course fails because it breaks several things about JavaScript syntax. The array is only the square brackets and everything that’s contained within.

1 Like

this
return videos[index];

to this?

return ["0dgNc5S8cLI","mnfmQe8Mv1g"];[index];

No, not like that. You’re nearly there. Can you spot where you went wrong there?

1st removed videos
return [index];

Next the array
["0dgNc5S8cLI","mnfmQe8Mv1g"];

Goes where videos once was.
return ["0dgNc5S8cLI","mnfmQe8Mv1g"]; [index];

Like this?
return ["0dgNc5S8cLI","mnfmQe8Mv1g"] [index];

Some of what you said is the array, actually isn’t.

Just this?

"0dgNc5S8cLI","mnfmQe8Mv1g"

Like this?
return "0dgNc5S8cLI","mnfmQe8Mv1g"; [index];

Now you have removed the square brackets which are also part of an array. Those shouldn’t be removed.

Like this?

return ["0dgNc5S8cLI","mnfmQe8Mv1g"]; [index];

When I said that the array is the square brackets and that which is contained inside of them, why are you also including stuff that is outside of the array?

1 Like

My approach here might not seem to be all that helpful, but I’m attempting to educate you about some fundamentals of JavaScript that you don’t understand.

2 Likes

Only this?
"0dgNc5S8cLI","mnfmQe8Mv1g"

That is the contents of the array. The array itself include square brackets around it.

This was step one.

Replace videos from the return with the array.

You said only replace videos, you didn’t say to do anything besides that.

1 Like