0

I am practicing for course work and wanted to make a simple code to cycle through each string in an array. However, what actually happens is that it displays every string in the array over and over instead of replacing the one colour for another. How do I do this?

<!DOCTYPE html>
<html>
    <head>
        <title>Arrays</title>
        <script>
            colours = [" Purple"," Blue"," Pink"," Red"," Green"];
            function change() {
                for (i = 0; i < colours.length; i++) {
                    document.getElementById("hai").innerHTML += colours[i];
                }
            }
        </script>
    </head>

    <body>
        <p>The colour is: <span id=hai></span></p>
        <button id="btn" onclick="change()">Change!</button>
    </body>
    </html>

colours = [" Purple", " Blue", " Pink", " Red", " Green"];

function change() {
    for (i = 0; i < colours.length; i++) {
        document.getElementById("hai").innerHTML += colours[i];
    }
}
<p>The colour is: <span id=hai></span></p>
<button id="btn" onclick="change()">Change!</button>

3

4 Answers 4

6

This is because you're appending the innerHTML of the node, rather than simply setting it, like so:

document.getElementById("hai").innerHTML = colours[i];

However, I suspect that's still not quite what you want - I'm guessing you want a new color after every click, in which case the code ought to look something like:

var colours = [" Purple"," Blue"," Pink"," Red"," Green"];
var currentColourIndex = 0;
function change() {
    document.getElementById("hai").innerHTML = colours[++currentColourIndex % colours.length];
}

Edit: JSFiddle example

Sign up to request clarification or add additional context in comments.

3 Comments

Neat trick with the modulus. I never would have thought of that.
Good use of fake accounts
Next time explain better what you need. I grow downvotes even answering your exactly question
3

You need an index, declared and initialized outside the change function:

var actualIndex = 0,
    colours = [" Purple", " Blue", " Pink", " Red", " Green"];

function change() {
    actualIndex++;                 // increment
    actualIndex %= colours.length; // correct the range
    document.getElementById("hai").innerHTML = colours[actualIndex];
}
<p>The colour is: <span id="hai"></span></p>
<button id="btn" onclick="change()">Change!</button>

2 Comments

This is exactly the same as Thomas' answer.
not really, i keep the index low, the other answer increments until it never goes.
2

In this case you do not need a for-loop: you want display just 1 color at time.

var colours = [" Purple", " Blue", " Pink", " Red", " Green"];
var currentIndex = 0;

function change() {
  if(currentIndex >= colours.length) currentIndex = 0;
  document.getElementById("hai").innerHTML = colours[currentIndex++];
}
<p>The colour is: <span id=hai></span></p>
<button id="btn" onclick="change()">Change!</button>

Comments

0

For loop can't be used for your requirement. Use the below snippet.

    <script>
        colours = [" Purple"," Blue"," Pink"," Red"," Green"];
        var count=0;
        function change() {
        if(count==5){
            count=0;
        }
        document.getElementById("hai").innerHTML = colours[count];
        count++;
        }
    </script>

Comments

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.