Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I just wanted to ask a simple question about MongoDB and javascript (express js).

Here are my requirements:

I am trying to create a back-end service for films and series. I created most of the basic routes and functions. I want to pick a film from the database for the "Movie Of The Day" subtitle.


I wrote a function that can run after midnight (00:00) but when I refresh the page it keeps continue to get random data.

How can I create a function that can pick a movie randomly just once a day?

Do you want me to extend my question with my routes? Let me know.

What I have tried:

JavaScript
My Function:

    function filterFilmsForImbd(films,imbd){
        let filmChances = [];
        films.filter(film=>{
            if(film.imbdScore >= imbd){
                filmChances.push(film);
            };
        });
        return filmChances;
    };
    
    //Movie Of The Day
    function movieOfTheDay(films) {
        const date = new Date();
        if (date.getHours() >= 0) {
            let arr = filterFilmsForImbd(films,7)
            let numberOfFilmChances = arr.length;
            let randomFilmPicker = Math.floor(Math.random() * numberOfFilmChances);
            return arr[randomFilmPicker]//returns object (film object)
        };
    };
    
    //Recommended Films
    function recommendedMovies(films){
       return  filterFilmsForImbd(films,5);
    };
Posted
Updated 2-Feb-21 19:44pm
v3
Comments
oLiontas 3-Feb-21 2:41am    
You must set a variable counter to localstorage. Lets say true or false. If true then you can run the function if false stop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900