Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

Chetan Jaisinghani
Chetan Jaisinghani
2,896 Points

What am I doing wrong here?

In this video, Dave created a function and the for loop was then modified so that values for red, blue and green each come from calling the function. Instead of:

red = randomRgb ();

I want to change it in a way that I can pass arguments to parameters in the function like this:

randomRgb(red);

What am I doing wrong here?

var html = '';
var red;
var green;
var blue;
var rgbColor;
var color;

function colorBlocks(color){
  color = Math.floor(Math.random() * 256 );
  return color;
}

for (var i = 0; i < 10; i += 1) {
  colorBlocks(red);
  colorBlocks(green);
  colorBlocks(blue);
  rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
  html += '<div style="background-color:' + rgbColor + '"></div>';
}

document.write(html);

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

You don't need to send any parameters to function. If you want to generate color, just create variables, like in code below

var html = '', red, green, blue, rgbColor;

function colorBlocks(){ return Math.floor(Math.random() * 256 ); }

for (var i = 0; i < 10; i ++) {
red = colorBlocks(); 
green = colorBlocks(); 
blue = colorBlocks(); 
rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; 

html += '<div style="background-color:' + rgbColor + '; height:50px; width:100%"></div>';
} 
document.write(html);

May the Force be with you, jung Padawan ;)

Chetan Jaisinghani
Chetan Jaisinghani
2,896 Points

This is what was shown in the video. However, my question was, how can I 'change' it in order for it to work the way I want it to (i.e. by sending arguments to parameters)?

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

You don't need to send arguments, this function return value. What do you mean change it in order? RGB model always take Red first, Green second and Blue third, that why it calls RGB model

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Or you can do it without creating variables:

var html = '';

function colorBlocks(){ return Math.floor(Math.random() * 256 ); }

for (var i = 0; i < 10; i ++) {
html += '<div style="background-color: rgb(' + colorBlocks() + ',' + colorBlocks() + ',' + colorBlocks() + '); height:75px; width:100%"></div>';
} 
document.write(html);