I’ve been competing the Udemy course The Web Developer Bootcamp. I’ve upload a completed code along exercise; this one is a guessing game of the RGB colour code.
Check out the completed example here.
This exercise taught a number of key skills. How to use for loops to update arrays.1
2
3
4
5
6
7
8for(var i = 0; i < squares.length; i++) {
if(colors[i]){
squares[i].style.backgroundColor = colors[i];
} else {
squares[i].style.display = "none";
}
}
How to make random numbers into an array.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22function generateRandomColors(num){
//make an array
var arr = []
//repeat num times
for(var i = 0; i < num; i++) {
arr.push(randomColor())
//get random color and push into arr
}
//return that array
return arr;
}
function randomColor() {
//pick a "red" from 0 - 255
var r = Math.floor(Math.random() * 256)
//pick a "green" from 0 - 255
var g = Math.floor(Math.random() * 256)
//pick a "blue" from 0 - 255
var b = Math.floor(Math.random() * 256)
return "rgb(" + r + ", " + g + ", " + b + ")";
}
The examples full code can be found on Github.com.