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

Where am I going wrong

//Create a condtional where //if the randomNumber variable is equal to 1 set theAnswer variable to "perhaps it is so", //if randomNumber is equal to 2 set theAnswer variable to "yes, definitely" and //if randomNumber is equal to 3 set theAnswer to "no, never"

const randomNumber = Math.floor((Math.random() * 3) + 1);

let theAnswer = "";

if(randomNumber === 1) { console.log ("perhaps it is so"); } else if(randomNumber === 2) { console.log("yes, definitely"); } else { console.log("no, never "); }

When I run the code it does what it's suppose to do but it's also printing False. Eg. False perhaps it is so.

1 Answer

Hi Gilbert.

The issue with your code is that you're using console.log to directly print the answers ("perhaps it is so," "yes, definitely," or "no, never") inside the conditional statements. This results in the answers being printed along with "False" because console.log returns undefined, which is being printed by default.

const randomNumber = Math.floor((Math.random() * 3) + 1);

let theAnswer = "";

if (randomNumber === 1) {
    theAnswer = "perhaps it is so";
} else if (randomNumber === 2) {
    theAnswer = "yes, definitely";
} else {
    theAnswer = "no, never";
}

console.log(theAnswer);

Kind regards, Berian

Got it, Thanks!