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) Working With Numbers The Random Challenge Solution

corey plummer
corey plummer
1,584 Points

How's this???

Here's the code that I put. Plus somethings I had issues with...

1) Even though I understand what each piece does, I'm not fully sure on how to put each component together to create the End Goal. ( I had to watch the solution video to figure out how to)

2) The code gave me issues in the Chrome Console, but not in Work Spaces. Any reasons why?

3) When I use alert() the <p> tags show up in the message, but when i use document.write() the <p> tags do not show up. I don't understand why that is.

var usersNumber = prompt("Pick any number!");
var usersNumberConv = parseInt(usersNumber);
var randomNumber = Math.floor(Math.random() * usersNumberConv)+ 1; 
var message = "<p>" + randomNumber + " is a randon number between 1 and " + usersNumberConv + ".</p>";
document.write(message);

Thanks,

Corey

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, it's not uncommon to struggle with how to put all these pieces together. You've learned a lot about variables, data types, concatenation, loops, arrays (and the list goes on). The best way to learn all this and learn to apply it is to practice. There's just no way around that.

As for your second point, no workspaces isn't going to show any errors with your javascript. Javascript is an interpreted language as opposed to a compiled language. Because javascript is interpreted by the browser and run in the browser (as opposed to workspaces) any errors will only show up in the browser console.

And for your third point. the <p> tag is an html element. When you use the <p> tag to write to an HTML document you are creating a paragraph. But the alert prints out a string literal. It doesn't use HTML.

Hope this helps! :sparkles:

corey plummer
corey plummer
1,584 Points

So, I can have errors and it will still work? I don't know if I can wrap my mind around that...

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That's not what i meant. :smiley: I said that errors won't show in the workspaces because the code isn't interpreted in workspaces.

Let's put it this way. You type the code in workspaces. But it's not until it makes it to the browser that it's interpreted. It's only there that any errors will show.

If you were doing a compiled language like C#, Java, or Python then yes, those errors would come up at compilation in the console part of workspaces. But JavaScript isn't compiled, so while coded in the workspace, it isn't executed or interpreted inside the workspace.