I am using an array that has some objects in JavaScript, which is then displayed onto an HTML element, and this works by having them in a for loop and the class changes if the team WON a match and if the team LOST a match.
But, I was wondering if there is a way for me to change the long for loop to forEach.
Below is my HTML code and JavaScript code.
HTML:
<div class="form-guide">
<div class="england-form-guide" id="englandFormGuide">
<p class="scores" id="scores"></p>
</div>
</div>
JavaScript:
var guide = [{
result: "won",
match: "England 2-1 Belgium",
},
{
result: "lost",
match: "England 0-1 Denmark",
},
{
result: "won",
match: "England 3-0 Republic of Ireland",
},
{
result: "lost",
match: "Belgium 2-0 England",
},
{
result: "won",
match: "England 4-0 Iceland",
}
]
var text = "";
for (var i = 0; i < guide.length; i++) {
text += guide[i].match + "<br>";
if (guide[i].result == "won") {
text += '<span class="colour-green"><span class="green-circle">W</span>' + guide[i].match + '</span><br>';
} else if (guide[i].result == "lost") {
text += '<span class="colour-red"><span class="red-circle">L</span>' + guide[i].match + '</span><br>';
}
}
document.getElementById("scores").innerHTML = text;
Many Thanks.
forloop.forcan be interrupted withbreak(forEachcan't) and you can useawaitinside it (although in your current code you don't need this).