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 trialLuqman Shah
3,016 PointsConfused with the second challenge
Everything was going great until he started going through the second challenge, like what just happened?? I understand math objects, methods, objects and properties, and I understood the way he did the first challenge, it made sense. But the second challenge was so confusing and was only briefly touched upon, then the video was over. Usually when I get confused with a code challenge I at least have an idea of what I'm specifically stuck with so I can explain myself better, with examples as well..
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 PointsI was confused too, as in I had no idea what just happened. Then I took a break and came back and watched it over (3 times).
I don't know how to explain it without repeating Dave. Try watching the solution video from 3:23 to 3:53, and pause in between to follow along. That's what eventually helped me understand. Good luck!
1 Answer
Paul Scott
15,403 PointsHere is some code that'll hopefully help you and others understand what Dave did for the second challenge. I was also super confused about this too but broke it down until it clicked.
Example: User inputs 10 for the starting number and 25 for the ending number.
1. The user inputs 10 for the starting number for the range.
var input1 = prompt('Starting Number?');
2. Then parseInt
changes the input1
value from a string to a integer.
var firstNum = parseInt(input1);
3. User inputs 25 for the ending number for the range.
var input2 = prompt('Ending Number?');
4. Then parseInt
changes the input2
value from a string to a integer.
var endingNum = parseInt(input2);
5. Takes the starting number and subtracts it by the ending number. Then it adds 1 so it won't be from 0 to -25 but from -1 to -25.
var calc1 = firstNum - endingNum + 1;
6. Generates a random float number between 0 and up to but NOT including 1. Then it multiples that by the calc1
variable to make it a random float number that generates from -1.0 to -25.0.
var randomNum = Math.random() * calc1;
7. Takes the random float number from -1.0 to -25.0 and rounds it down to intergers that'll range from -1 to -25, no decimals.
var floorNum = Math.floor(randomNum);
8. Makes it a positive integer by adding the ending range number 25
to your random floor number. This Changes your negative integers to positive integers. It'll now range from 1 to 25. Feel free to try it in a calculator. Example: -6 + 25 = 19
var output = floorNum + endingNum;
9. The <p></p>
are paragraph tags from HTML. It'll display on it's own line because it is a "Block-level element".
The concatenation is performed to form a sentence to show the results from the output
variable and to include the user's input value and output value.
var message = "<p>" + output + " is a number between " + firstNum + " and " + endingNum + "." + "</p>";
10. Finally, use the message
variable inside the write()
method to display the results.
document.write(message);
Thanks Steven Parker for the Markdown tip.
Steven Parker
231,275 PointsWhen posting code, 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.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsI'm confused now also — what is it that you are asking?