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 trialClara Marquina
Courses Plus Student 5,189 PointsHow to test a random condition?
Hi there!
When using a random condition how can I test that the if statement works? I'll explain my question with the example from the exercise of the video:
Here's my code:
//variables
var randomNumber = Math.floor(Math.random() * 6) +1;
var guess = prompt("Guess my number between 1 and 6");
parseInt.guess
document.write("<p>" + "My number was " + randomNumber + "</p>");
//conditional statement
if (randomNumber === guess) {
alert("Wow!That's amazing!")
} else {
alert("Sorry!Try again!")
}
I tried several times until the random number matched the guess I introduced, to test that if the condition was true, the program will run properly. But, what happend when you have to guess between 1 - 1000, for example? Do you have to run your code until you matched the random condition to see if it works?? I guess not, but I can't figure out how to do it...
Thanks!
Aurelian Spodarec
10,801 PointsHI Clara, it's over 6months since you started, can I ask you what are you doing now and how are you with progrmming? :)
2 Answers
Billy Bellchambers
21,689 PointsHi Clara,
For this simple example yes it does just require you to keep attempting to guess the value to see if your code works.
Alternatively you could temporarily set the value for var randomNumber if your just trying to test the responses from your if, else statement.
Like so.
//variables
var randomNumber = 8; // was Math.floor(Math.random() * 6) +1;
var guess = prompt("Guess my number between 1 and 6");
parseInt.guess
document.write("" + "My number was " + randomNumber + "");
//conditional statement
if (randomNumber === guess) {
alert("Wow!That's amazing!")
}
else {
alert("Sorry!Try again!")
}
This was you can plug in values to test you if else code whilst knowing the randomNumber value.
Later in this topic you elaborate on this process so this should hopefully give you more insight into random numbers and guessing.
Additionally looking at your code the parseInt.guess is not the correct way of using parseInt. See below.
parseInt(guess) //the standard function
//in your code whilst the function is incorrect even being correct wouldnt serve any purpose as the function is not being used in the if statement or even updating the variable to a number. can be used in two ways giving the same results.
//1. update the guess variable with parseInt() result
guess = parseInt(guess); //this will then use the new value of guess in your if function
// or 2
if (randomNumber === parseInt(guess)) { //again achieving the same result but including the parseInt() function only in the if statement leaving the variable guess unchanged.
alert("Wow!That's amazing!")
}
else {
alert("Sorry!Try again!")
}
Hope that helps you.
Clara Marquina
Courses Plus Student 5,189 PointsThank you Billy! I knew there should be a way to test it, seem quite logical now :) Thanks for the additional correction!
theofanissiassios
6,498 PointsYou could add
console.log(randomNumber);
And look in the console for the random number that has been generated.
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 Points//Fixed Code Presentation