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

时间:2017-09-30 01:26:10

标签: javascript jquery arrays sorting alphabetical

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

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

        <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>

2 个答案:

答案 0 :(得分:-1)

好像你有favWords而不是favPlaces

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

答案 1 :(得分:-1)

因为没有定义favWords。也许你的意思是favPlaces.join(&#34;,&#34;)并尝试更改$(&#39; #outputPlaces&#39;)。html = favPlaces.join(&#34;,&#34;)to document.getElementById(&#34; outputPlaces&#34;)。innerHTML = favPlaces.join(&#34;,&#34;)