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

Valeriya Voloshina
Valeriya Voloshina
5,943 Points

My code doesn't work

Hey guys! I posted my code below in this question. I don't see if it's any different from the one shown in the video yet mine does not seem to work. Could anyone help me?

Thanks!

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

edited code for formatting.

Marshall Vaughn
Marshall Vaughn
4,016 Points

Same issue for me. Code matches the video exactly. I duplicated the file structure locally and ran it, and it worked fine. Must be something wrong with the workspace.

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Try Math.rand() rather than random. Watch that the syntax highlighting changes colour to indicate that you used the right method.

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

I just ran your code in codepen, and it worked fine. I would check to see if you have your JS linked to the HTML.

Valeriya Voloshina
Valeriya Voloshina
5,943 Points

Thank you for taking interest! It was linked fine so must be something else then :/