Javadeus Stardate 93718.37 Destination Math.floor

In Javascript, there are several Math functions that are built into the language to carry out programming functions in the script.

var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt (‘I am thinking of an number between 1 and 6. What is it?’);
if (parseInt(guess) === randomNumber ) {
document.write(‘<p>You guessed the number!</p>’);
} else {
document.write(‘<p>Sorry. The number was ‘ + randomNumber + ‘</p>’);
}

In this sample script, we are declaring a variable called randomNumber and generating a random number using the math.random function and requesting an input prompt.

There is an else conditional statement that checks the output and determines if the statement “You guessed the number” or “the number was ‘another number'” is stated by the script as an end result of the program.

 

Leave a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s