   Wise men speak because they have something to say; Fools because they have to say something.- Plato (427-347 B.C.) News Tutorials Code Samples Blogs Forums ### Learning JavaScript Basics

Page: 6 of 10
(View All)

#### Math Object

The math object is a built in object which includes math constants and functions. Theres no need to create any math object, because it already exists automatically when using JavaScript.

There are four useful functions in JavaScript:
• Math.random() - Will generate a random number between 0 and 1.
• Math.ceil() - Will round your number up to the next integer.
• Math.floor() - Rounds your number down to the previous integer.
• Math.round() - Round your number to the nearest integer.

Make sure when using the math functions, be sure to capitalize the 'M' - Just to brief you more, here's what each function would do with the number 3.23:
Math.ceil(); would increase the number to 4.
Math.floor(); would decrease the number to 3.
Math.round(); would make the number 3. But say if the number was 3.5, the function rounds automatically to the nearest even integer.

Below is a line of code using Math.floor and Math.random that generates a number between 1 and 10.

Lets put the math.random to use on a large scale. From the code sample below, we're going to generate 7000 random numbers. We'll also keep a random total and calculate the average. And we'll use the Math.Round() to calculate the average to the thousandths place. Check it all out below:

This is a perfect example why you should use loops. Imagine having a user input 7000 numbers, or even hand writing them yourself. Let alone finding the average!

#### IF STATEMENT

This is where you will use your conditions. You set a condition in the program and have it perform certain tasks according to the condition.

Here is the syntax of an If statement in JavaScript:
if(<condition>)
{
statement1
statement2
statement3
..so on...
}

*If you are only going to use one statement in JavaScript, then you will not need { and }. These are used for conditions with multiple statements. If you put multiple statements and do not use { and }, then only the statement executed will be the statement immediately following the condition.

Now here's a practical example of using an "if" condition:

So what do you think will happen in that condition? Well, the condition is true: x is not equal to 24, so the values of a, b and c will equal what was assigned to them.

#### IF/ELSE STATEMENT

In the examples above, the condition was true. But what if it was not? Then what would happen? If you only had an If statement, nothing would happen. It would skip the statements between { and }, and proceed to execute the rest of the program. But what if you wanted something else to occur if it was not true? This is where you will use an If/Else statement.

Here is the syntax of an If/Else statement in JavaScript:

if (<condition>)
{ //used only for multiple condition circumstances statement1;
statement2;
} else { statement1; statement2; }

*Again, if you're only going to use one statement for if, and one for else- the { and } are not required.

Here is a practical example with an If/Else condition:

So, what do you think happened here? x is = to 99, so the statements for true are executed. y = y + 10 and z = z / 14.

### INPUTTING DATA

You can input data directly into your JavaScript program from what is called the Window.prompt dialog box. This dialog box is displayed with a caption you specify, and two buttons (ok and cancel). Automatically, the information entered will be considered a string. So, if you want to change the value to a number, just use the parseInt statement. The argument of the statement is the caption you put in the box. Like, "what is the answer?"

syntax of using window.prompt:

<a_string> = window.prompt(<the caption you want to appear>);

example of using it in a program:

The "if" statement checks to see if the variables match, if they do- that means the person entered the correct answer. If not, they answered incorrectly or did not enter a numerical value. Technically, you could run another if statement to check if the results were = to "NaN" then if that was true, make them enter an actual numerical value.

#### Window.confirm

In addition to window.prompt, there is window.confirm(); The difference is that it will display two buttons, 'Ok' and 'Cancel' which can be used for an if statement for True or False results.

Check out the syntax:

OurVariable = window.confirm("Message you want displayed.");

Look at the code below to see exactly how it works:

The message "Do you like spring" is displayed in a dialog box that appears when the page is loaded. If the user selects 'Ok' then the program will print out, "I like spring as well." If the user selects 'Cancel' then the program will print out, "Why don't you like spring?" on the screen.

Now on to a simple program using a bit of what we covered.

 1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  | Next »

You Must be logged in or a member to comment. #### Tutorial Stats

409,403 Views
4.66 Rating of 5 (3 Votes) #### Tutorial Options

· Read more by this author #### Related

· 