Search code examples
javascriptjqueryarrayssortingalphabetical

alphabetically sort an array of inputted words with javascript/jquery


I am new to javascript and jquery. I need help figuring out what is wrong with my code because it is not executing ".sort()" and ".join(", ") during the last steps...

I want the page to have a pop-up box where the user types in a place (inputs are stored in the array) and continues that until they type 'done', and then the page loads their list of places in alphabetical order with commas. I got the first part ok (the prompts and entering text), but my page doesn't load the entered material after entering 'done'. Below is my code:

        <div id="outputPlaces"></div>

        <script>
            $(document).ready(function() {
                var favPlaces = [];
                var input = prompt("Please enter your favorite place or type done to stop entering places.");
                while (input != 'done') {
                    favPlaces.push(input);
                    input = prompt("Please enter another favorite place or type done to stop entering places.");
                }
                favPlaces.sort();
                $('#outputPlaces').html = favPlaces.join(", ")

            });

        </script>

Solution

  • Seems like you had favWords instead of favPlaces.

    .html(favPlaces.join(", "))