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 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,976 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. :)