3. Write a JavaScript code that print 3 color (yellow, black, red). You should create an array that has all the color then use a loop to print the array.
Required program in Javascript -->
<!DOCTYPE html>
<html>
<body>
<script>
var colors = ["yellow","black","red"]; //colors are stored in array
colors
for(var i=0;i<3;i++){ // for loop starts from index 0 to index 2
(since there are only 3 elements)
document.write(colors[i]+" "); // this will print the element of an
array with a space
}
</script>
</body>
</html>
Program first defines an array and give colors to the array. Then for loop will start from index 0 to index 2 and will print colors[i] and a space each time for loop is executed.
Get Answers For Free
Most questions answered within 1 hours.