Are 2 Math.floor's needed or necessary in this code?

With Code 2, would you be able to help me remove this duplication from it?

Would you be able to walk me through the steps to do that?

It should be able to work without needing this piece…

https://jsfiddle.net/3aj02byv/

Just this top piece:

Would you be able to say if that is possible to do?

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

    const mvideos = videos[Math.floor(Math.random() * videos.length)];
    document.querySelector(".video").setAttribute("data-id", mvideos);
}());

you would need to write code so that mvideos will not be able to have previous value.

What where you using before to avoid duplication?

This was the code.
https://jsfiddle.net/7zd3yebn/


  const videoID = [
        "0dgNc5S8cLI",
        "mnfmQe8Mv1g",
        "CHahce95B1g",
        "2VwsvrPFr9w"
    ];

    function onPlayerReady(event) {
        event.target.loadVideoById(videoID[currentVideoId]);
        player.setVolume(100); // percent

    }

    function onStateChange(event) {
        if (event.data == YT.PlayerState.ENDED) {
            currentVideoId = Math.floor(Math.random() * videoID.length);
            player.loadVideoById(videoID[currentVideoId]);
        }
    }

can you explain it? do you understand what was used to avoid duplication?

what’s the exact point in your code that make so that the previous used value can’t be reused this time?

I want to work on removing the top piece because that was originally what I was trying to figure out how to do.

https://jsfiddle.net/3aj02byv/

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

    const mvideos = videos[Math.floor(Math.random() * videos.length)];
    document.querySelector(".video").setAttribute("data-id", mvideos);
}());

Rest of the code:

(function manageCover() {

    function show(el) {
        el.classList.remove("hide");
    }

    function hide(el) {
        el.classList.add("hide");
    }

    function coverClickHandler(evt) {
        const cover = evt.currentTarget;
        hide(cover);
        const video = cover.parentElement.querySelector(".wrap");
        show(video);
    }

    const cover = document.querySelector(".jacket");
    cover.addEventListener("click", coverClickHandler);
}());

const videoPlayer = (function makeVideoPlayer() {

    const videos = [
        "0dgNc5S8cLI",
        "mnfmQe8Mv1g",
        "CHahce95B1g",
        "2VwsvrPFr9w"

    ];
    let videoList = [];


    let player = null;

    const tag = document.createElement("script");
    tag.src = "https://www.youtube.com/iframe_api";
    const firstScriptTag = document.getElementsByTagName("script")[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    function onPlayerReady(event) {
        player = event.target;
        player.setVolume(100); // percent
    }

    function onStateChange(evt) {
        if (evt.data)

        {
            return;
        }

        if (!videoList.length) {
            videoList = videos.filter(function (a) {
                return a !== player.getVideoData().video_id;
            });
        }


        const id = videoList.splice(Math.floor(Math.random() * videoList.length), 1)[0];
        player.loadVideoById(id);
    }

    function addPlayer(video) {

        const config = {
            height: 360,
            host: "https://www.youtube-nocookie.com",
            videoId: video.dataset.id,
            width: 640
        };

Do you want to avoid duplication or not?

Removing this top piece would be removing the duplication from it.

Right, or no?

Yes I want to, but I also want it to work the same way as this code does.

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

    const mvideos = videos[Math.floor(Math.random() * videos.length)];
    document.querySelector(".video").setAttribute("data-id", mvideos);
}());

you want to remove the videos array?

That’s the main duplication part, right, so then yes.

so you have a different source for the ids?

I don’t know your code, but you can certainly have one single array available to multiple places of your code

Let me see, I think so.

This one uses a different source, right?
https://jsfiddle.net/89pxe24u/

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
        };

I would need to look carefully at all the versions of code you have posted until now. I don’t know

Would this one be better?
https://jsfiddle.net/op7u1ta9/


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,
            width: 640
        };

better in what sense? what’s the difference with the other one?

This one uses a function:


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

This one doesn’t use a function:

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

didn’t you just remove that function in the other topic?

but still, what do you mean with better?

The code I’m removing duplication from, uses both.
The duplication part is the function part that is being removed.

A function can always be added on and removed.

We’ll keep the array in the code that doesn’t use a function.

https://jsfiddle.net/3aj02byv/

This will stay:

    const videos = [
        "0dgNc5S8cLI",
        "mnfmQe8Mv1g",
        "CHahce95B1g",
        "2VwsvrPFr9w"
           
    ];
    let videoList = [];

I’m really at a loss, there is seems to be difficulty communicating.

Good luck with your project.