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 trial

JavaScript JavaScript Loops Working with 'for' Loops The Refactor Challenge – Duplicate Code

Eduardo Vargas
Eduardo Vargas
5,871 Points

Just 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
Steven Parker
229,744 Points

A 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.