JavaScript How to sort keys in arrays according to frequency sort keys array php,sort keys array javascript,sort array

My submit button is finally working. However, it is not counting all of the votes in the results. How do I get my submit button to sort all of the votes?

HTML:

    <!DOCTYPE html>
    <html>
    <link rel="stylesheet" text="text/css" target='_blank' href="movies.css">
      <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>

    <title> 
    Movie List 
        </title>
      </head>
      <body>
        <h1>My Favorite Movies</h1>
            <div id = "first">
                <input type="text" id="movie" placeholder="Movie">
                <button id="enter">Enter</button>
            </div>
      <div id="left">
        <div id="list"><u>Chosen Films:</u></div>
        <!-- <div>Chosen Films: <span id="list"></span></div> -->
        <!-- <div id="films"></div> -->
        <div id="best"><u>You're Best Films:</u></div>
        <button id="submit">Submit</button>
      </div>

        <div id= "results"><u>Results</u></div>       

    <script type="text/javascript" src="movies.js"></script>
      </body>
    </html>

Javascript:

$(document).ready(function() {
    var films = [];
    // with working vote button
    $("#enter").click(function () {
        var movie = $("#movie").val();
        var $btn = $('<button />', {
            id: "vote",
            type: "button",
            text: "Vote",
            value: movie,
            click: function(){
                var vote = this.value;
                $("#best").append('<p>'+vote+'</p>');
                films.push(vote); 
            }
        });

        var $p = $('<p />');
        $p.append($btn, " ", movie);
        $p.appendTo("#list");
    });
});

So here is where the trouble is:

This code should allow the submit button to take the listed films under "your best films" and arrange them in a third list under "results" in order of their votes.

$("#submit").click(function () {
    var ballot = {};
    for (var i = 1; i < films.length; i++) {
        var key = films[i];
        ballot[key] = (ballot[key] || 0) + 1;
    }
    var elect = [];
    for (key in ballot) elect.push({key: key, freq: ballot[key]});
    elect.sort(function(a,b){return b.freq - a.freq});
    console.log(elect);
    for (var i = 0; i < elect.length; i++){
        console.log(elect[i].key);
        $("#results").append('<p>' + elect[i].key + '</p>');
    }
});

Any suggestions or ideas would be appreciated!

Answer:1

I have two components: <div> <advertiser-add-component></advertiser-add-component> <advertisers-component></advertisers-component> </div> These components ...

I have two components: <div> <advertiser-add-component></advertiser-add-component> <advertisers-component></advertisers-component> </div> These components ...

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

  1. how to prevent drug abuse
  2. what prevents animal abuse

I want to get today's date + 7 days. Today I get with public dateToday: string = new Date().toLocaleDateString('de-DE'); On Google I found this: this.dateOneWeek.setDate(this.dateOneWeek.getDate() +...

I want to get today's date + 7 days. Today I get with public dateToday: string = new Date().toLocaleDateString('de-DE'); On Google I found this: this.dateOneWeek.setDate(this.dateOneWeek.getDate() +...

  1. ionic date from string
  2. ionic date to string format
  3. ionic get date from string