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) Making Decisions with Conditional Statements Build a Random Number Guessing Game

Steven Cousineau
Steven Cousineau
2,563 Points

if statement will not execute!

Hello everyone, I'm kinda confused at the moment why my code will not execute the if statement, I've tryed the code in another editor and the same result. I have it logging the randomNumber in the console to make sure i can get the right answer to test as you can see. I would appreciate any input and I thank you!

_My workspace code here->https://w.trhou.se/9pmqp7wbwg

Hello, Steven.

You're problem seems to be: var randomNumber = console.log(Math.floor(Math.random() * 6) + 1); The correct way is to just make it this: (Math.floor(Math.random() * 6) + 1);

2 Answers

Michael Lambert
Michael Lambert
6,286 Points

Hia,

It seems that the random number can't return a number to a variable and print a number to the console in one statement. The variable randomNumber never receives the random number which is why the 'if' part isn't working. Don't know how you could achieve both at the same time in this manner.

Steven Cousineau
Steven Cousineau
2,563 Points

Thank you for your reply. You both are right it seems as i rewrote the whole code wiithout the console.log and it worked. Which is weird because the first time I tried executing the original code didn't work, which is why I implemented the console.log to test the code more efficently. That was the idea anyways, regardless thank for your time guys!

Hello, Steven.

You're problem seems to be: var randomNumber = console.log(Math.floor(Math.random() * 6) + 1); The correct way is to just make it this: (Math.floor(Math.random() * 6) + 1);