I have an array of test scores that need to be the color red if they are under a 70 percent so I created an array and a for loop to pick those out of this array. However when I try to set those array elements to the color red I get this error. I am a bit new to JS so it could be an easy fix.
The array works and it will print the scores under 70 in the console log if however I can not get them to be red. Any ideas?
Uncaught TypeError: Cannot set property 'color' of undefined at index.html:23
<script>
var scoresArray = [84, 72, 56, 84, 0, 76, 72, 68, 70];
for (var i = 0; i < scoresArray.length; i++) {
if(scoresArray[i] < 70) {
var c = scoresArray[i];
c.style.color = "red";
}
}
testgen(scoresArray);
</script>
The function testgen is called from an external script.
function testgen(scoresArray) {
document.write("<li class=\"special\">");
for (var i=0; i<scoresArray.length; i++) {
document.write("<li>" + scoresArray[i] + "</li>");
}
document.write("</li>");
}
c.style.color = "red";wherecis a number in your array. You can only set the style of an element, not a number