Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge

analyn jane calado
analyn jane calado
3,523 Points

any comments??

my work space is not working!

Math.floor(Math.random() * (6 - 1 + 1)) + 1; 
function randomNumber(top, bottom) {
    var randomMath = Math.floor(Math.random() * (top - bottom + 1)) + bottom; 

 if (parseInt(randomMath) > parseInt(bottom)){
        alert("you guessed larger number");

    }else {
        alert("you guessed smaller number ");
    }
}
randomNumber(100, 20);

2 Answers

Dan Hedgecock
Dan Hedgecock
13,807 Points

I would recommend being more careful about how you format your code. You're missing a semi-colon, but it's hard to tell the way you've written it.

Your code works when I try it in the console with a semi-colon after your randomNumber declaration.

// This line isn't needed, it will execute and not impact your function randomNumber
//Math.floor(Math.random() * (6 - 1 + 1)) + 1;

function randomNumber(top, bottom) {
  var randomMath = Math.floor(Math.random() * (top - bottom + 1)) + bottom;
  if (parseInt(randomMath) > parseInt(bottom)) {
    alert("you guessed larger number");
  }else {
    alert("you guessed smaller number ");
  }
};

randomNumber(100, 20);
analyn jane calado
analyn jane calado
3,523 Points

no, it's okay for me whatever you want to say about my code..

Dan Hedgecock
Dan Hedgecock
13,807 Points

It's just a suggestion, I've had plenty of times where a long function will throw an error because of a missing semi-colon or closing brace. Keeping your format spaced out and tidy will really help with syntax errors and higher order functions when you start writing complex logic. I had to learn that the hard way! -Good Luck!

analyn jane calado
analyn jane calado
3,523 Points

okey, thanks.. hmp, maybe i pressed the delete in my keyboard, and i didn't knew it...

I formatted your code into a code block. Once I did that, I could see that you weren't actually missing a semicolon anywhere!

The reason this doesn't work is becauseparseInt is used to convert a string into an integer. In this case, you can't use it on randomMath because that will already be an integer (because Math.floor() returns an integer). It also won't do anything if bottom is a number, as it would when you call randomNumber(100, 20) at the end of your code.

In this challenge you were only meant to output the generated random number so the user could see it. It wasn't meant to be a guessing game.

If you take out the entire conditional statement (if ... else) and instead just return randomMath, then at the end you can use alert(randomNumber(100, 20)); and you should get the random number between 20 and 100 in a pop-up box.