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, Part II Solution

Aaron Stevens
Aaron Stevens
2,470 Points

Only one number generated and no thrown error message..Help?

My code is identical to the video solution, but the console only shows one number and no error message no matter how many times I call the function (correctly or incorrectly). It seems like the code for the generator is the only thing passing for some reason. Help would be appreciated!

my code:

function getRandomNumber (lower, upper){
  if (isNaN(lower) || isNaN(upper)) {
    throw new Error('Both arguments must be numerical values.');

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

}

console.log( getRandomNumber(1, 5) );
console.log( getRandomNumber(5, 15) );
console.log( getRandomNumber(1, 'twenty') );
Sean T. Unwin
Sean T. Unwin
28,690 Points

Which browser are you using and how are you running the code, e.g. Workspace, Broswer Dev Tools, CodePen?

3 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

I'm not really familiar with Safari, but is it possible you have error warnings turn off in the console?

For the record, your code works fine with Chrome's and Firefox's Dev Tools, although when I tried it using Firefox's Scratchpad from the Dev Tools the error displayed in the Scratchpad, but not the console. It worked from the default input within the console with Firefox, however.

Aaron Stevens
Aaron Stevens
2,470 Points

browser: Safari Editor: Workspace

I have not tried to run it anywhere else as of yet.

Aaron Stevens
Aaron Stevens
2,470 Points

Thanks for the quick response. I ran it in the safari console without workspaces and it worked as it should. Not sure what the issue was in regards to why the code wouldn't work in workspace, but as long as it's correct and runs elsewhere I am fine with it. Appreciate it!