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

rgb parens

The code below is refactored to call the randomValue() function on rgb which is assigned to const color.

Why does rgb require a parens () in order for randomValue() to run? (i. e. rgb( function calls() )

function randomRGB() { const color = rgb(${randomValue()}, ${randomValue()}, ${randomValue()}); return color; }

1 Answer

Steven Parker
Steven Parker
231,172 Points

To invoke any function (make it run) you place parentheses after its name, whether or not you pass it any arguments inside the parentheses.

This rule is the same in JavaScript and CSS.

Steve,

Thanks for taking a look at this. To clarify: I was asking about the rgb assigned to const color. Is that what you were addressing? I understand that the function randomRGB requires parens to invoke. I don't see where 'rgb' is a function in the code.

Is that making more sense? Sorry if I'm not being terribly clear. Thanks!

Steven Parker
Steven Parker
231,172 Points

The "rgb" used here is a CSS function . It's a built-in standard in the browser. But CSS functions use parentheses in the same way as JavaScript functions. Plus, in this case they also contain 3 arguments (the color values) being passed to the function.

Ah - I see. I don't have much experience with CSS and, as you noted, it was clear to me that I was passing args into the parens. I had heard that CSS utilizes functions but I haven't done that myself. Thank you for clarifying!