2

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>
1
  • very beautifully written post, well done! Commented Apr 1, 2014 at 4:41

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

2 Comments

nicely explained! only one tiny thing missing: edit the last line so it appears as code (add 4 spaces)... ;)
.join was perfect and the simplest. Also, I didn't need the nbsp. I just put the space in as .join(" ").
1

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.

7 Comments

Thanks. What should I use for less than or equal to instead of <=
@AdrianPreuss You're not really explaining stuff, just plain say don't do this or that... Why is innerHTML bad...? Why use < rather than <=...? I think @codedigger explains it the best
@AdrianPreuss Yes, dude, I can read, but can you read your own answer...? You write It's a bad idea. While you putting Data with innerHTML, the browser rendering it. - this is no explanation, sorry, also makes no sense at all...
@AdrianPreuss Additionally, Dont use <= in your while loop. Thats not an valid index. - again, this doesn't make sense, plus isn't true either: 1. <= is not an index, but an operator, and 2. it is valid! have a read here
@AdrianPreuss You should explain that in your answer, not in a comment, especially considering the asker is a beginner. They shouldn't just follow your lead because you say so, but should understand why...
|
1

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

3 Comments

irrelevant. It does not matter, for which method you choose.
@AdrianPreuss agreed 100%
I voted up your post because I agree, but I thought it was worth showing that a for loop is more commonly used when looping over arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.