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 trialKimberly Dolcin
4,369 PointsHello 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
Kimberly Dolcin
4,369 PointsThank you so much for explaining Steven, this is the link to the challenge https://teamtreehouse.com/library/javascript-basics/creating-reusable-code-with-functions/random-number-challenge
Kimberly Dolcin
4,369 Pointsyes i watched the next video and didint understand....
Steven Parker
231,275 PointsSometimes, 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
4,369 Pointsthank 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
231,275 PointsWhen posting code, be sure to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Â 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
4,369 Pointsi dont understand your explanation but thanks for trying
Steven Parker
231,275 PointsLet 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 ( than there are closes ) — 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.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsI 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.