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 Introducing Functions

Why is it not working this way?

function alertRandom(){
  var randomNumber = Math.floor( Math.random() * 6 ) + 1; 
  return randomNumber;
};
alertRandom();
alertRandom();
alertRandom();
alertRandom();
alertRandom();



/* Is it necessary to add "alert(randomNumber)" command 
on line 3 and if yes what does this command convey?  */
function alertRandom(){
 var randomNumber = Math.floor( Math.random() * 6 ) + 1;
 return randomNumber; }; 
// here you call the function,
// but it's not yest displaying to the page
alertRandom(); 
//not sure how you are actually displaying it to the page
//if you want it to alert to the page you would do this
alert(alertRandom());
function alertRandom(){
  //You have stored your random number in the variable "randomNumber"
  var randomNumber = Math.floor( Math.random() * 6 ) + 1; 

  /*
  * Using "return" will display the random number in your browsers javascript console 
  * but not on the webpage or in a pop-up box as we have done in previous excercises
  */
  return randomNumber;

  /* 
  * Use the alert() function to display the random number in a popup. Now when you 
  * call your alertRandom() function it will in turn call the alert() function to display 
  * your number.
  */
  alert(randomNumber);
};
alertRandom();
alertRandom();
alertRandom();
alertRandom();
alertRandom();

If you have found my answers helpful please give a +1

because you just put return inside the function, to use the function with return just call it like this ::

<script>
alert(alertRandom);
</script>

1 Answer

Sergio Niño
seal-mask
.a{fill-rule:evenodd;}techdegree
Sergio Niño
Full Stack JavaScript Techdegree Student 22,975 Points

Hi, Atul R the error is because the semicolon at the end of the closing curly braces. use it, just when you're using the other way to create a function called: function expression

I hope this helps clear it up. :)