I have an array this.colors = [RED, BLUE, GREEN], and sometimes I would like to pick one random color from this array.
When I doing it by this way, the result is normal:
rand_color = this.colors[Math.floor(Math.random() * this.colors.length)]
javascript: console.log(rand_color)
// => rgb(211, 65, 65)
But when I've wrapped it in the function:
this.pick_random_color = function() {
return this.colors[Math.floor(Math.random() * this.colors.length)];
}
that function doesn't return random value. Instead, I get this message in the log:
color = this.pick_random_color;
javascript: console.log(color);
// => this.pick_random_color = function() {
// => return this.colors[Math.floor(Math.random() * this.colors.length)];
// => }
What's wrong with the function?
thisis obfuscating your code, it is totally unnecessary. DaveShaw is 100% right, you are assigning a null variable tocolorin this case.