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 Solution

Grzegorz Sypek
Grzegorz Sypek
9,311 Points

Why my code doesn't work in console.log but in window.alert work fine?

function getRandomNumber( lower, upper) {

return Math.floor(Math.random() * (upper - lower + 1)) + lower; }

alert( getRandomNumber(1, 6) ); alert( getRandomNumber(1000, 10006) ); console.log( getRandomNumber(0.5, 0.9) ); console.log( getRandomNumber(3, 5) );

Adam Beer
Adam Beer
11,314 Points

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```
function getRandomNumber( lower, upper) {
  return Math.floor(Math.random() * (upper - lower + 1)) + lower; 
}

alert( getRandomNumber(1, 6) ); 
alert( getRandomNumber(1000, 10006) ); 
console.log( getRandomNumber(0.5, 0.9) ); 
console.log( getRandomNumber(3, 5) );

3 Answers

Adam Beer
Adam Beer
11,314 Points

Your code is absolute correct. The first 2 alert prompt write on the window the 2 results. The second 2 console.log methods write the result in the console. Open the console with F12 if you use chrome or right click and select the investigation. You can use it Ctrl+Shift+I.

Grzegorz Sypek
Grzegorz Sypek
9,311 Points

My problem was with the Chrome browser. The console function doesn't write me result because it was marked as " hidden" in some strange way. Now it works fine but it was good to get an answer from you. Thanks Adam.

console.log() doesn't work for me in either Safari or Chrome. Not sure what you mean about "hidden"..

Adam Beer
Adam Beer
11,314 Points

Please show we your whole code.

I'm not sure what the problem was. I moved on with the other work on the track and today saw you reply. Went back and ran the code and now it is working in both. I'm not sure what the problem was. Maybe I wasn't using the console right at the time.