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 Basics (Retired) Creating Reusable Code with Functions Random Number Challenge

Kimberly Dolcin
Kimberly Dolcin
4,369 Points

Hello the challenge is to write a create a function between 2 upper and lower numbers e.g(between 1-10 and 2-17 than dis

than display it on the screen, what is wrong with my code please? function randomness(lowernum1-highernum1),(lowernum1-highernum2) { (Math.floor(Math.random() * (num1 - num2) + 1); (Math.floor(Math.random() * (num3-num4)+4);} alert(randomness(2-9),(8-19));

4 Answers

Steven Parker
Steven Parker
231,007 Points

I originally overlooked the fact that the system had provided a "watch video" button for us in the upper right of the page. The next video should help.

Kimberly Dolcin
Kimberly Dolcin
4,369 Points

yes i watched the next video and didint understand....

Steven Parker
Steven Parker
231,007 Points

Sometimes, particularly in the early stages, you might need to just forge ahead for a bit to expose yourself to additional examples in the other lessons until you get that "light bulb moment" when things suddenly start to make sense.

Also don't do too much at a time. Those moments can sometimes occur when you're doing something else!

Kimberly Dolcin
Kimberly Dolcin
4,369 Points

thank you you are right, i was struggling with html and css but now i love it! I do sometimes run into walls but i got that "lightbulb moment" and btw what would be your definition of "doing too much at a time"? Like how much is to much? I just want to practice as much as i could so i could change careers.... Im trying too put as much hours as possible...

Steven Parker
Steven Parker
231,007 Points

When posting code, be sure to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Even with it not formatted, I see a few issues:

  • in a function definition, the parameters should be enclosed in one set of parentheses
  • only the parameter names should appear, you can't do any math operations on them there
  • parentheses must be balanced within each statement (same number of opens and closes)
  • you must store, pass, or "return" any calculation for it to have any effect on the program
Kimberly Dolcin
Kimberly Dolcin
4,369 Points

i dont understand your explanation but thanks for trying

Steven Parker
Steven Parker
231,007 Points

Let me see if I can break it down, and using code formatting:

So on this line:

 function randomness(lowernum1-highernum1),(lowernum1-highernum2) {

All the parameters should be in the same set of parentheses, separated by commas (no subtracting here), and I'll guess you didn't mean to use the name "lowernum1" twice, so it might look like this:

 function randomness(lowernum1, highernum1, lowernum2, highernum2) {

Then on the next lines:

  (Math.floor(Math.random() * (num1 - num2) + 1);
  (Math.floor(Math.random() * (num3-num4)+4);

Each line has unmatched parentheses — there are more opens :point_right: ( than there are closes :point_right: ) — they must balance out. Also, when you do calculations like these, nothing happens unless you also do something with the result: either store it in a variable, pass it to a function, or "return" it.

Another problem I did not mention the first time is that these names (num1, num2, etc) have not been defined and they are different from the names of the parameters.

Then on the final line:

alert(randomness(2-9),(8-19));

This calls your function but with only one argument, negative seven (-7) resulting from subtracting 9 from 2, and passes the result (which will be "undefined", since the function does not return anything) to "alert". The second argument (negative 11) is ignored because alert only accepts one argument.

Try going on to the next video and look at the code the instructor shows there. The code in the video demonstrates these points and creates a functional sample program.