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) 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);