[SOLVED] Can this code be written without it beng a function?

Does this have to be a function?
https://jsfiddle.net/ynb24ds8/

I wrote this part as being a function, but can it work in the code without it being one?

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

javascript

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

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

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

Have you tried doing it?

Here I have:
https://jsfiddle.net/5w86o7h9/1/

This was my attempt.

 const videos = ["0dgNc5S8cLI", "2VwsvrPFr9w"];
    const index = Math.floor(Math.random() * videos.length);

    function addPlayer (videos){

        const config = {
            height: 360,
            host: "https://www.youtube-nocookie.com",
            videoId:  videos[index],
            width: 640
        };

and what’s the result in doing that? does it work? what does it do? what should it do?

It does not work, I get this error.

An error occurred. Please try again later

Do you have more infos than that? What is it that gives that error?

I know there is nothing wrong with the ids and that they work.

["0dgNc5S8cLI", "2VwsvrPFr9w"];

Awesome, and the other things?

The code still doesn’t work as written though.

This may not be written correctly.

 const videos = ["0dgNc5S8cLI", "2VwsvrPFr9w"];
    const index = Math.floor(Math.random() * videos.length);

    function addPlayer (videos){

        const config = {
            height: 360,
            host: "https://www.youtube-nocookie.com",
            videoId:  videos[index],
            width: 640
        };

Have you tried adding console.log somewhere in your function?

1 Like

Now I know a function is required there and it can’t work like the way I had it.

No, the function is not required, have you tried to debug why this doesn’t work?

Try using console.log with the variables you have (videos, index) in any place they are present (after being defined, before being used) and see if you can see what’s wrong

1 Like

This:


console.log(videos, index);

Where? And what does it say in the console?

I don’t understand what it is saying.

[object HTMLDivElement] {
  accessKey: "",
  addEventListener: function addEventListener() { [native code] },
  after: function after() { [native code] },

it’s an HTML element, in particularly a div, with all the stuff attached.

Is that what you expect from the videos variable?

1 Like

I really don’t know. All I know is, the way it is set up without a function, that way it doesn’t work.

Is that what you expect from the videos variable?

To answer your question, then, no.

so you don’t know if that is the value you expect from the videos variable

you wrote this so you were certainly expecting a certain value from videos.

If it’s not that, as this is a different scope, it means that there is a different variable defined with the same name which values come from somewhere else than

Can you find it?

1 Like

This works:
https://jsfiddle.net/kh1sdz3v/

const videoId = ["0dgNc5S8cLI", "2VwsvrPFr9w"];
    const index = Math.floor(Math.random() * videoId.length);

    function addPlayer (videos){

        const config = {
            height: 360,
            host: "https://www.youtube-nocookie.com",
            videoId:  videoId[index],
            width: 640
        };

good, do you understand why the other doesn’t work?

1 Like