1

This is literally the first time I've attempted coding - I don't even know if I will make any sense in my questions, please bear with me

Anyways, I've written a code that loops and displays an array.

Is it possible to change the colour of the text for each loop?

Thanks for the help everyone :)

<!DOCTYPE html>
<html>
<body>

<h2>Heroes & Their Ratings</h2> 
<p><font color="red">This will display some superheroes and their respective ratings:</font></p>
<p id="demo"> </p>


<script> //Starts loop script

var heroes = ["Batman 3", "Superman 4", "Wonderwoman 7", "Flash 10", "Spiderman 17", "Green Lantern 18", "Bat Girl 28","Thor 23","Captain 30","Punisher 2", "Blink 1","Amber 5","Spawn 6","Dart 8", "Destiny 9","Iceman 11", "Magma 12","Dr. Fate 13","Grifter 15","Ice 14","Katana 16","KidFlash 19","Nite Owl 20","Red Arrow 21","Raven 22","V 25","Wildcat 24","Zan 25","TNT 26","Terra 27"]; //Array so heroes variable stores multiple names
var text= '';
var i = 0 //Assigns i a value of 0
for (; i < heroes.length; i++) { // loop runs for every superhero
text += heroes[i] +  "<br>";
}
document.getElementById("demo").innerHTML = text;

//Ends loop script </script>



</body>
</html>
1
  • You should usually declare your counter variable in the for statement itself: for (var i = 0; i < heroes.length; i++) Commented Oct 25, 2017 at 20:19

3 Answers 3

4

Yes, there are plenty of ways. One of them is using font tag as you used before:

var heroes = ["Batman 3", "Superman 4"];
var colors = ["blue", "green"];
var text= '';
for (var i = 0; i < heroes.length; i++) { // loop runs for every superhero
  text += "<font color='"+colors[i]+"'>"+heroes[i] +  "</font><br>";
}
document.getElementById("demo").innerHTML = text;
<!DOCTYPE html>
<html>
<body>

<h2>Heroes & Their Ratings</h2> 
<p><font color="red">This will display some superheroes and their respective ratings:</font></p>
<p id="demo"> </p>

</body>
</html>

However font tag is Not Supported in HTML5. So a better way would be:

text += "<span style='color:"+colors[i]+"'>"+heroes[i] + "</span><br>";

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

2 Comments

Thank you so much, I really appreciate you helping a beginner like me <3
No problem, hope you continue coding ^^
0

If you are beginner I can show you easy solution for it.

var colors = ["black","red","blue"...] //make colors array and enter color values for each hero
for (var i = 0; i < heroes.length; i++) {
  text += "<font color='"+ colors[i] +"'>"+ heroes[i] +  "</font><br>";
}

Comments

0

If you associate color with your hero name, then it would become simpler to identify or modify easily the name for each hero.

    var heroes = [{name:"Batman 3", color:"green"}, {name: "Superman 4", color:"brown"}];
    for (var i=0; i < heroes.length; i++) {
    	document.getElementById("demo").innerHTML +=  "<div style='color:"+heroes[i].color+"'>" + heroes[i].name +  "</div>";
    }
    <html>
    <body>
    <h2>Heroes & Their Ratings</h2> 
    <p><font color="red">This will display some superheroes and their respective ratings:</font></p>
    <p id="demo"> </p>
    </body>
    </html>

Comments

Your Answer

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