简体   繁体   中英

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>

好像你有favWords而不是favPlaces

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

Because favWords is not defined. Maybe you mean favPlaces.join(", ") and try changing $('#outputPlaces').html = favPlaces.join(", ") to document.getElementById("outputPlaces").innerHTML = favPlaces.join(", ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM