最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript While Looping Through Array And Outputting in .innerhtml - Stack Overflow

programmeradmin5浏览0评论

I'm brand new to Javascript and teaching myself. I'm trying to setup a function that will write each of the elements of the array AmericanCars with a space between the elements.

I have it working so that when I use .innerHTML=AmericanCars[Index Position] plus a non-breaking space, I can write one element of the array. Now I'm trying to loop through it with a while loop, but I just get an output of "undefined."

How can I get .innerHTML to write out each of the elements of an array with a space between them? Is there a method that is simpler than using the While loop or is my While loop just formatted wrong.

<html>
    <head>
    </head>
    <body>

        <script language="javascript" type="text/javascript">
        var AmericanCars=["Chevy", "Ford", "Dodge"];
        var JapaneseCars=["Honda", "Toyota", "Subaru"];
        var FavoriteCars=[];

        function ListAmericanCars() {

        var arraycounter = 0; 

        /* Use array counter to set the value that is displayed in innerHTML's square [] brackets */
            while (arraycounter <= AmericanCars.length) {

                document.getElementById('content').innerHTML = AmericanCars[arraycounter] + "&nbsp ";
                arraycounter++;
            }
        }
        </script>

        <p> What Are Your Favorite American Cars?  Answer: 
            <span id="content">______________
            </span>
        </p>

        <button onclick="ListAmericanCars()">
            American Cars Function
        </button>
    </body>
</html>

I'm brand new to Javascript and teaching myself. I'm trying to setup a function that will write each of the elements of the array AmericanCars with a space between the elements.

I have it working so that when I use .innerHTML=AmericanCars[Index Position] plus a non-breaking space, I can write one element of the array. Now I'm trying to loop through it with a while loop, but I just get an output of "undefined."

How can I get .innerHTML to write out each of the elements of an array with a space between them? Is there a method that is simpler than using the While loop or is my While loop just formatted wrong.

<html>
    <head>
    </head>
    <body>

        <script language="javascript" type="text/javascript">
        var AmericanCars=["Chevy", "Ford", "Dodge"];
        var JapaneseCars=["Honda", "Toyota", "Subaru"];
        var FavoriteCars=[];

        function ListAmericanCars() {

        var arraycounter = 0; 

        /* Use array counter to set the value that is displayed in innerHTML's square [] brackets */
            while (arraycounter <= AmericanCars.length) {

                document.getElementById('content').innerHTML = AmericanCars[arraycounter] + "&nbsp ";
                arraycounter++;
            }
        }
        </script>

        <p> What Are Your Favorite American Cars?  Answer: 
            <span id="content">______________
            </span>
        </p>

        <button onclick="ListAmericanCars()">
            American Cars Function
        </button>
    </body>
</html>
Share edited Sep 17, 2016 at 10:19 Andy Jazz 59.3k18 gold badges162 silver badges256 bronze badges asked Apr 1, 2014 at 4:23 user3483571user3483571 251 silver badge3 bronze badges 1
  • very beautifully written post, well done! – benomatis Commented Apr 1, 2014 at 4:41
Add a ment  | 

3 Answers 3

Reset to default 3

Arrays are 0-based. Change it to less than or else the last element will print undefined.

while (arraycounter < AmericanCars.length) {
    ...
}

Additionally, you could just do:

document.getElementById('content').innerHTML = AmericanCars.join('&nbsp');

It's a bad idea. While you putting Data with innerHTML, the browser rendering it.

A better solution is to create an variable and append the data:^

var AmericanCars=["Chevy", "Ford", "Dodge"];
var JapaneseCars=["Honda", "Toyota", "Subaru"];
var FavoriteCars=[];

function ListAmericanCars()  {
    var arraycounter = 0; /* Use array counter to set the value that is displayed in innerHTML's square [] brackets */
    var myContent = '';

    while (arraycounter < AmericanCars.length) {
        myContent += AmericanCars[arraycounter] + "&nbsp ";
        arraycounter++;
    }

    document.getElementById('content').innerHTML = myContent;
}

EDIT: Ah forgotten. Dont use <= in your while loop. Thats not an valid index.

I think a for loop would more standard in this situation, also build up your content and then assign it to innerHTML. Array indexes start at 0 and not 1, so you should use < and not <=', else you will be off by one at the end of your loop.

function ListAmericanCars() {
    var content = '';
    for (var i = 0; i < AmericanCars.length; i++) {
        content += AmericanCars[i] + "&nbsp ";
    }
    document.getElementById('content').innerHTML = content;
}
发布评论

评论列表(0)

  1. 暂无评论