Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Analysis

This script demonstrates an output of both HTML and dialog boxes. The HTML output serves the base conversion table, whereas the alert boxes are the output of single-value conversion operations.

power(op1, op2)

// Simulate the "power of" (^) operator
/////////////////////////////////////////

function power(op1, op2) {
 var result = 1
 for (var i = 1; i <= op2; i++) {
result *= op1
 }
 return result
}

This function returns the value op1op2. The function is called with op1 and op2 values as parameters. There are a few restrictions on the value of op2:

  • It must be an integer.
  • It must not be negative ([rdblarr] positive or 0).

These restrictions are not important for us because all our calculations follow both rules.

The power() function uses a loop, although it can be programmed to use recursion as well. The recursive variant is:

function power(op1, op2) {
 if (op2 == 0)
   return 1
 else
   return power(op1, op2 – 1) * op1
}

This variant works because:

power(x, 0) = 1

power(x, y) = power(x, y – 1) * x

In its iterative version, the script assigns an initial value of 1 to result. It then iterates through a loop, multiplying result by the loop’s counter value on each iteration, until the counter is equal to the second operator (or parameter). The local variable result holds the accumulated result throughout the entire function.

div(op1, op2)

// Simulate the div (integral division)
// operator
/////////////////////////////////////////

function div(op1, op2) {
 return Math.round(op1 / op2 – op1 % op2 / op2)
}

The div() function was presented in Chapter 7, Utilizing JavaScript Operators. It simulates the div (integral division) operator utilized in many other programming and scripting languages. The calculation is very simple:

op1 / op2 – op1 % op2 / op2

The first element (op1 / op2) is the quotient of the regular division operator. The second term represents the fractional part of the quotient, and therefore it is subtracted from the quotient. The modulo operator returns the remainder of the division as a whole number, so it is divided by the second operand to receive the fractional part of the quotient.

When we first designed this function, this was the only process implemented on the operands. However, we soon found that the function returned strange results, such as 2.9999... . The obvious conclusion was that the division and modulo operators returned inaccurate results. We chose to include the round method of the Math object (don’t worry, it’s explained later in the book) to round the result off to the nearest integer. We then had the perfect function, as we had anticipated.

The div operation can be simulated with other techniques, such as the Math.floor() method, that will be explained later under Math object.

getDigit(val)

// Returns a digit (maximum hexadecimal)
// based on its decimal value
/////////////////////////////////////////

function getDigit(val) {
 if (val == 10) return "A"
 if (val == 11) return "B"
 if (val == 12) return "C"
 if (val == 13) return "D"
 if (val == 14) return "E"
 if (val == 15) return "F"
 return val
 // the return statement terminates the function,
 // so there is no need for else statements
}

// Returns the decimal value of a digit
// (maximum hexadecimal)
/////////////////////////////////////////

This function accepts an integer between 0 and 15, and returns its equivalent hexadecimal (base 16) number. Since there are 16 hex digits, the returned value is always a hexadecimal. The function uses consecutive if statements, each one of which, if it evaluates to TRUE, can terminate the function. Returning from the middle of the function is preferred, as far as performance is concerned. Many of the possible arguments handed off to this function (all the numbers between 0 and 9) are to be returned without being converted, as their hexadecimal and decimal representations are identical.

At first, the function checks whether the value of the parameter val is 10. If it is, the function returns the number’s hexadecimal representation, "A". If not, JavaScript continues to the “B” statement, and beyond. The process continues until a true condition is met, or until JavaScript has passed all if statements without any true condition. It indicates that the value of val is not between 10 and 15, meaning it is between 0 and 9. The function returns the exact same value.

getValue(dig)

// Returns the decimal value of a digit
// (maximum hexadecimal)
/////////////////////////////////////////

function getValue(dig) {
 if (dig == "A") return 10
 if (dig == "B") return 11
 if (dig == "C") return 12
 if (dig == "D") return 13
 if (dig == "E") return 14
 if (dig == "F") return 15
 return dig
 // the return statement terminates the function,
 // so there is no need for else statements
}

The getvalue() function may be considered the opposite of getDigit(value), because it accepts a hexadecimal number (actually, it must be a digit), and returns the argument’s decimal value. This structure of this function is identical to getDigit()’s.

For example, if dig == "B", the function returns 11, which is B’s (or b’s) decimal representation.

Note that both getValue() and getDigit() work with systems that do not exceed 16. That is, the maximum base is hexadecimal, so they only work with digits up to “F”.

toBase(num, base)

// Convert from decimal to specified base
/////////////////////////////////////////

function toBase(num, base) {
 var newNum = (num == 0) ? "0" : ""
 while (num >= 1) {
  newNum = getDigit(num % base) + newNum
  num = div(num, base)
 }
 return newNum
}

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us