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 trialAtul R
3,446 PointsWhy 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? */
Nathan Crum
15,183 Pointsfunction 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
Martinus Lukas
Courses Plus Student 3,023 Pointsbecause 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
Full Stack JavaScript Techdegree Student 22,976 PointsHi, 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. :)
john larson
16,594 Pointsjohn larson
16,594 Points