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

Opinions on completed coding task

https://w.trhou.se/v27hynd2zc

Just completed this code exercise and would like to get opinions on the method and practice used.

Context: Initial code had the loop missing and was instead the initial code repeated 10 times. The task was to refactor the code using a loop.

Outcome: The task was completed successfully. All I am now wondering is if there was be any advantage or disadvantage in using a different loop method and if in any way there are any forms of bad practice used.

Thnaks

https://w.trhou.se/v27hynd2zc

Further help if possible please.

I have attempted to shorten the code by creating a function so I don't have to repeat the Math.random variable inside the for loop. This is my current and when i check there is nothing on the screen but the console gives me errors. could someone please explain.

Steven Parker
Steven Parker
231,007 Points

The function is a good idea, but that's the same snapshot link as before. To show the new code, make a fresh snapshot and paste the new link it gives you here. I'll bet it's something easy to fix once we see it.

3 Answers

Steven Parker
Steven Parker
231,007 Points

Instead of passing the variables to the function, you'll need to assign them with the function's return value:

  red = randomColour();

Then, inside the function, you need an assignment operator ("=") instead of a comparison ("==").

But you can also eliminate the variable completely and pass back the expression result directly:

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

perfect. thank you

Steven Parker
Steven Parker
231,007 Points

charles bempah — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!