Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEduardo Vargas
5,871 PointsJust need varification on my understanding of Value() and why randomValue function doesn't have parentheses.
After staring at Guil's solution (especially the value() final step) for about an hour, I think I finally understand it.
let html = '';
let randomValue = () => Math.floor(Math.random() * 256);
function randomRGB(value) {
const color = `rgb( ${value()}, ${value()}, ${value()} )`;
return color;
}
for (let i = 1; i <=10; i++ ) {
html += `<div style="background-color: ${randomRGB(randomValue)}">${i}</div>`;
}
document.querySelector('main').innerHTML = html;
However, I do have a question. I noticed that the function randomValue here does not have parentheses when called...
for (let i = 1; i <=10; i++ ) {
html += `<div style="background-color: ${randomRGB(randomValue)}">${i}</div>`;
}
Is that because Guil had already added the parentheses next to the "value" placeholders in the variable "color"...or is there another reason?
function randomRGB(value) {
const color = `rgb( ${value()}, ${value()}, ${value()} )`;
return color;
}
It's a confusing last step and any help understanding it would be appreciated. Thanks in advance.
1 Answer
Steven Parker
231,248 PointsA function name is always followed by parentheses when called. Without them, it isn't being called but a reference to it is being passed as an argument. This is often referred to as a "callback".
The randomValue
function doesn't get called until the point where you see value()
in the other function.