使用 javascript/jquery 按字母顺序对输入的单词数组进行排序

alphabetically sort an array of inputted words with javascript/jquery

我是 javascript 和 jquery 的新手。我需要帮助找出我的代码出了什么问题,因为它在最后几步没有执行“.sort()”和“.join(”, “)……

我希望页面有一个弹出框,用户可以在其中输入一个地方(输入存储在数组中)并继续输入,直到他们输入 'done',然后页面加载他们的列表以逗号字母顺序排列的地方。我的第一部分没问题(提示和输入文本),但我的页面在输入 'done' 后没有加载输入的 material。下面是我的代码:

        <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(", "))

因为没有定义 favWords。也许您的意思是 favPlaces.join(", ") 并尝试将 $('#outputPlaces').html = favPlaces.join(", ") 更改为 document.getElementById("outputPlaces").innerHTML = favPlaces.join(", ")