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, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

Konrad Dziekonski
Konrad Dziekonski
7,798 Points

my solution

Hello,

Could you please comment on my solution? it basically works, I have created it after the first video challange before the first solution, but after the first solution when we were asked to get rid of repeating code attached to red, green and blue variables i couldnot come up with an idea as in rgb synthax there are three numbers eg. rgb (xx, yy, zz) and all of them had to be randomly picked so I did nothing. After watching the solution I know I should have this instint of putting more complex things into variables, but perhaps You as more experienced developers will poin out some wrongs in my conception.

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

function picker() {
    red = Math.floor(Math.random() * 256 );
    green = Math.floor(Math.random() * 256 );
    blue = Math.floor(Math.random() * 256 );
  return rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
}
for (let i=0; i<100; i+=1) {
html += '<div style="background-color:' + picker() + '"></div>';
}
document.write(html);

also one last question if I may, about this print function that Dave had written, I have tried it myself because I was curious if it would work when I leave the brackets empty, and I know by now that it would not, and that there have to be anything as long as it matches what is inside the document.write brackets eg:

function print(x) {
document.write(x);
}
print(html);

so this x a kind of declaration of some sort? Why can we not leave it empty? Sorry if that do not make any sense but I dont feel quite comfortable with the topic and I am trying to grasp the conception.

1 Answer

Parameters are treated as local variables. If you don't pass in a value the parameter has a default value of undefined. You can specify a default value with the assignment operator.

function print(x = ""){
    document.write(x);
}