Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [139]

By Root 1328 0
an integer value. If the string has a floating-point representation (“4.3” for example) an integer value (4) is returned.

♦ parseFloat() converts text to a floating-point value.

♦ toString() takes any variable type and creates a string representation. Usually, using this function isn’t necessary to use because it’s invoked automatically when needed.

♦ eval() is a special method that accepts a string as input. It then attempts to evaluate the string as JavaScript code and return the output. You can use this method for variable conversion or as a simple calculator — eval(“5 + 3”) returns the integer 8.

♦ Math.ceil() is one of several methods of converting a floating-point number to an integer. This technique always rounds upward, so Math.ceil(1.2) is 2, and Math.ceil(1.8) is also 2.

♦ Math.floor() is similar to Math.ceil(), except it always rounds downward, so Math.floor(1.2) and Math.floor(1.8) will both evaluate to 1.

♦ Math.round() works like the standard rounding technique used in grade school. Any fractional value less than .5 rounds down, and greater than or equal to .5 rounds up, so Math.round(1.2) is 1, and Math.round(1.8) is 2.

Fixing the addInput code

With all this conversion knowledge in place, it’s pretty easy to fix up the addInput program so that it works correctly. Just use parseFloat() to force both inputs into floating-point values before adding them. You don’t have to explicitly convert the result to a string. That’s automatically done when you invoke the alert() method.

//

// from addInput.html

var x = prompt(“first number:”);

var y = prompt(“second number:”);

var sum = parseFloat(x) + parseFloat(y);

alert(x + ” plus ” + y + ” equals ” + sum);

//]]>

You can see the program works correctly in Figure 1-11.

Figure 1-11: Now the program asks for input and correctly returns the sum.

Conversion methods allow you to ensure that the data is in exactly the format you want.

Chapter 2: Making Decisions with Conditions

In This Chapter

Generating random numbers and integers

Working with conditions

Using the if-else and switch structures

Handling unusual conditions


One of the most important aspects of computers is their apparent ability to make decisions. Computers can change their behavior based on circumstances. In this chapter, you discover how to maximize this decision-making ability.


Working with Random Numbers

Random numbers are a big part of computing. They add uncertainty to games, but they’re also used for serious applications, such as simulations, security, and logic. Most languages have a feature for creating random numbers, and JavaScript is no exception. The Math.random() function returns a random floating-point value between zero and one.

Technically, computers can’t create truly random numbers. Instead, they use a complex formula that starts with one value and creates a second semi-predictable value. In JavaScript, the first value (called the random seed) is taken from the system clock in milliseconds, so the results of a random number call seem truly random.


Creating an integer within a range

Creating a random floating-point number between zero and one is easy, thanks to the Math.random() function. What if you want an integer within a specific range? For example, say that you want to simulate rolling a six-sided die. How do you get from the 0-to-1 floating-point value to a 1-to-6 integer?

Here’s the standard approach:

1. Get a random float between 0 and 1 using the Math.random() function.

2. Multiply that value by 6.

This step gives you a floating-point value between 0 and 5.999 (but never 6).

3. Use math.ceil() to round up.

At this point, you need to convert the number to an integer. In Book IV, Chapter 1, I mention three functions you can use to convert from a float to an integer. Math.ceil() always rounds up, which means you’ll always get an integer between 1 and 6.


Building a program that rolls dice

The following rollDie.html code helps you simulate rolling a six-sided die.

®Online Book Reader