Archive for May, 2007

Utilizing JavaScript Operators . 101 Although you will

Tuesday, May 8th, 2007

Utilizing JavaScript Operators . 101 Although you will probably never use bitwise operators, let s learn how the results are calculated. All calculations will be performed in hexadecimal and binary bases, because 7 they are most convenient for the task. Remember that hexadecimal numbers have a 0x prefix. Let s take a look at the following numbers, and how the bitwise AND operates on them: 0×23 001000112 & 0×72 011100102 = 0×22 001000102 Chapter 0×23&0×72//evaluatesto34(=0×22=2216) The bitwise AND operator is similar to the logical AND operator, which is discussed later in this chapter. You can use the bitwise AND operator to test whether a number is even or odd. In binary (base 2), the last digit of an odd number is 1, and the last digit of an even number is 0. The following function uses the bitwise AND operator to determine whether the number is odd or even. It returns true if decimalNumber is even, and false if it is odd. functioncheckEven(decimalNumber) { return(decimalNumber&1==0) } Don t worry if you are not familiar with the == equality operator. It is introduced later in this chapter. Come back to this script after we discuss the equality operator. Bitwise OR operand1|operand2 The OR operator, also known as the inclusive OR operator, compares its operands. It returns 1 if at least one of the compared bits is 1. Table 7-5 shows the operator s truth table. Table 7-5. Bitwise ORtruth table. Bit1Bit2Bit1|Bit2 0 0 0 0 1 1 1 0 1 1 1 1
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

100 . Chapter 7 Hexadecimal notation is convenient (Web site)

Tuesday, May 8th, 2007

100 . Chapter 7 Hexadecimal notation is convenient for representing binary data because each hexadecimal digit represents four binary bits. Table 7-2 lists the hexadecimal values from 0 to F along with the equivalent binary values. Table 7-2. Hexadecimal and binary equivalence. Hexadecimal Binary Hexadecimal Binary 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 Bitwise operators enable the scripter to work on individual bits. The bitwise (bit) operators in JavaScript are listed in Table 7-3. Table 7-3. Bitwise operators in JavaScript. Syntax Name Type & Bitwise AND binary | Bitwise OR binary ^ Bitwise XOR (exclusive OR) binary ~ Bitwise NOT unary << Left shift binary > Right shift binary >> Zero-fill right shift binary Bitwise AND operand1 & operand2 The bitwise AND operator compares two bits. The only situation in which the result is 1 is when both bits are 1. Here is the truth table for this operator: Table 7-4. Bitwise AND truth table. Bit1Bit2Bit1&Bit2000 0 1 0 1 0 0 1 1 1 The AND operator, like all other bitwise operators, can take only a numeric value as its operand.
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Utilizing JavaScript Operators . 99 //sequence#2document.write(”Ihave”) document.write(2) document.write(”cookies.”) (Business web hosting)

Monday, May 7th, 2007

Utilizing JavaScript Operators . 99 //sequence#2document.write(”Ihave”) document.write(2) document.write(”cookies.”) Acommon mistake made by beginners is to forget spaces in strings. Aspace is a character just like any other. Forgetting a space character is not a severe error, because you can easily locate where to add it. You should use one of the following statements to print two consecutive numbers with a separating space character in between: Chapter document.write(16+”"+18)//firstpossibilitydocument.write(16,”",18)//secondpossibility The first statement is valid because of the automatic casting method used by JavaScript s interpreter. . Tip: The plus (+) operator in JavaScript does not convert string operands to numeric values. The reason behind this is that the string concatenation operator and the plus operator use the same character: +. If only one operand is a string, the other is converted to a string value and then the operator concatenates the strings. In Perl, the plus (addition) and minus (subtraction) operators convert their arguments from strings to numeric values if necessary, and return a numeric result. This feature is made possible because the string concatenation operator in Perl is . (dot), not +, which is shared with another operator (addition) in JavaScript. C++ and Pascal feature a function that concatenates strings. Bitwise Operators First, let me state that bitwise operations can be confusing for many programmers. I should also note that a great many programmers work for years without ever having to use them. You can be a successful JavaScript programmer even if this section does not quite make sense to you. However, I would have been remiss not to have included it. Bitwise operators are the operators used in bit-oriented operations. Abit is the smallest unit of information, usually represented by 0 or 1. Bit manipulations are used to control the machine at the lowest level. If you plan to program at a higher level, this section may be safely skipped. In JavaScript you won t be using bitwise operators to control the machine at a low level but rather for other purposes such as encrypting and encoding. Eight consecutive bits form a byte. There are 256 (28) byte variations. That is, a byte can be one of 256 eight-bit sequences. For example, 11010001 is one of these 256 possibilities. Abyte is represented by a character in programming languages that support character data types, such as C, C++, and Pascal.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

98 . Chapter 7 mistake. Due to the (Web hosting resellers)

Monday, May 7th, 2007

98 . Chapter 7 mistake. Due to the internal structure of the JavaScript interpreter, and the negation operator specifically, negating a numeric value using the negation operator is faster than multiplying it by 1. If you are a traditional Pascal programmer, it might take you a while to get used to the negation and incremental operators, but it is worth the effort! vara=3 varb=9 a+b// evaluates to 6 b // evaluates to 9 String Operators operand1 + operand2 The string operator s syntax is identical to that of the addition operator. They differ in the type of operands they operate on. This operator accepts any values as operands, provided that at least one of them is a string. Astring is actually an object, so it can be said that the string operator operates on string objects. It joins them together, as in: “Ladies ” + “and” + “gentlemen” The string operator can operate on more than two operands, but it is still a binary operator, because of the way it works. It concatenates the first two strings, then concatenates the third string to the accumulated string, and so on. If one of the operands is not a string, it is automatically cast to a string. The string operator is also called a concatenation operator. An expression consisting of numerous string operators evaluates to a single string. Based on that, here are two different statements: document.write(”I have”+2+” cookies.”) document.write(”I have “, 2, ” cookies.”) At first, you might think that these statements are equivalent. They aren t, because the first one uses the string operator, and the second one uses commas to delimit strings and numbers. They differ more than in style. In the first statement, the expression between the parentheses is evaluated to a single string “I have 2 cookies.” Therefore, the document.write() method in this statement prints only one expression. The second statement prints multiple expressions. The literals are not evaluated to a single value as in the first statement but are rather printed independently. Both statements print the same HTML to the page, but they do it in different ways. In order to understand how each statement works, take a look at the following sequences of statements. The first sequence is equivalent to the first statement in the previous set, and the second sequence is equivalent to the second statement. // sequence #1 var stringToPrint = “I have”+2+” cookies.” document.write(stringToPrint)
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

Utilizing JavaScript Operators . 97 vara=1varb=aa++ document.write(”ais”,a,”,bis”,b)//ais2,bis1 The

Monday, May 7th, 2007

Utilizing JavaScript Operators . 97 vara=1varb=aa++ document.write(”ais”,a,”,bis”,b)//ais2,bis1 The increment operator can only be used with a variable or a property of an existing object, but not on a literal. It is natural to come to a conclusion that incrementing is the same as adding 1 to the value: Chapter vara=1varb=1a++ b=b+1//equivalenttob+=1(seeassignmentoperators) This is true as far as correctness of the script is concerned. It is incorrect if performance is important. The advantage of incrementing is that it is much faster than standard assignment (fourth line in above code section). You should always increment when you want to add 1 to a variable (or to a property of an object). It is not so important when the addition operation is done a few times. You will definitely feel the difference when you have 100,000 addition operations. Another benefit of the incrementing operator is that it is much easier to understand a statement like countTemp++ than countTemp = countTemp + 1. It is important to remember that Boolean expressions are equivalent to 1 and 0 in certain situations. The following statements show the effect of incrementing Boolean variables: vara=truevarb=falsea++ b++ document.write(”ais”,a,”,bis”,b)//ais2,bis1 Decrement operand1– –operand1 The decrement operator is similar to the increment operator. It decreases the value of the operand by 1, whereas the increment operator increases it by 1. Negation operand1 The negation operator precedes a numeric value (a variable, a property of an existing object, or a numeric literal). By placing this operator before its operand (do not insert any space characters), JavaScript evaluates a positive number as its corresponding negative number and vice versa. As before, you might think that this operator can be replaced by a statement in which the operand is multiplied by 1. Once again, this is a
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

96 . Chapter 7 Here are a few

Monday, May 7th, 2007

96 . Chapter 7 Here are a few examples: vara=div(23, 3) // a is assigned7 varb=div(12, 4) // b is assigned3 The function evaluates to the quotient of its arguments, with the remainder discarded. The sign of the result is the sign of the quotient. . Caution: Various operations on floating-point numbers return inaccurate results. This may seem strange, but such inaccuracies are a common phenomenon in computers. You should avoid floating-point values when possible, because such inaccuracies often result in unexpected behavior. Above all, debugging scripts that fail to work due to inaccurate calculations is nearly impossible. Increment operand1++ ++operand1 The increment operator is a unary operator, which can be used in either suffix or prefix notations. It increments the operand s value by 1. If used after the operand (suffix), the operator returns the value of the operand before incrementing it. If used before the operand (prefix), the operator returns the value of the operand after incrementing it. Understanding these differences is important when you use such operations as side effects of other statements, such as assignment statements. The following set of statements outlines this concept: vara=1 var b = ++a // prefix document.write(”a is “, a, “, b is “, b) // a is 2, b is 2 The first statement assigns the value 1 to a. The second statement performs two different actions: . Increments a to 2 . Assigns a s new value to b The increment operator in suffix notation performs the actions in reverse order, and therefore the results differ. Suffix notation is demonstrated in the following code: vara=1 var b = a++ // suffix document.write(”a is “, a, “, b is “, b) // a is 2, b is 1 b is assigned the value of a, and then a is incremented. Dual actions in one statement are discussed at the end of this chapter. Generally, you should avoid using such side effects. The previous code would be simpler had it looked like:
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

1 on 1 web hosting - Utilizing JavaScript Operators . 95 from floating-point division

Sunday, May 6th, 2007

Utilizing JavaScript Operators . 95 from floating-point division in that the result of integer division is always an integer number. JavaScript, on the other hand, does not explicitly distinguish between integers and real-valued numbers, and therefore, the result of a division operation is not guaranteed to be an integer number. In fact, most floating-point numbers are the result of a division operator. While debugging a script, it may be helpful to remember that the division operation in JavaScript generates the same value as your pocket calculator. You should also remember that the remainder of a division operation is never discarded. When the operands are floating-point numbers and cannot be represented in binary notation, division expressions often evaluate to inaccurate results. The following demonstrates the behavior of JavaScript s division operator: Chapter 3/4//evaluatesto0.753.6/0.1//evaluatesto36 20/4//evaluatesto 511.1/2.22//evaluatesto4.999999999999999 Modulus operand1%operand2 The modulus operator returns the remainder of a division operation. The division is performed, but only the remainder is kept. The sign of the result is the sign of the quotient. The modulus operator in JavaScript is also different from the one in other programming languages. It operates not only on integers but also on floating-point numbers. You should be aware that the modulus operator occasionally returns inaccurate results. The modulus inaccuracies stem from the division operation which sometimes returns inaccurate results: 12%5//evaluatesto212.3%4//evaluatesto0.3000000000000007(inaccuracy) 0%99//evaluatesto012.75%4.25//evaluatesto011.1%2.22//evaluatesto2.219999999999999 The Nonexistent Integral Division Operator JavaScript does not feature an integral division (also called div) operator. However, it is easy to create such an operator using simple arithmetic operators. The following function demonstrates this capability: functiondiv(op1,op2) { return(op1/op2 op1%op2/op2) { The keyword return instructs the function to return a value, so the function call itself evaluates to a value, just like an expression consisting of an operator. Now we can define the newly created div syntax: div(operand1, operand2)
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

Personal web server - 94 . Chapter 7 SyntaxNameType ++ Increment Unary

Sunday, May 6th, 2007

94 . Chapter 7 SyntaxNameType ++ Increment Unary Decrement Unary Negation Unary += Add then assign Binary = Assignment Binary = = Evaluation Binary Arithmetic operators take numeric literals, variables, or properties of existing objects as their operands. They always return a single numeric value, based on their operands values. Addition operand1 + operand2 The addition operator is a simple mathematical operator. It adds two numbers of any type and evaluates to their sum. 5+3// evaluates to 2 2.4 + 3.6 // evaluates to 6 1.1 + 7.8 // evaluates to 8.9 Subtraction operand1 operand2 Another simple mathematical operator is the minus operator. It subtracts one number from another. 8 2// evaluates to 6 16.3 56 // evaluates to 39.7 13.3 13.3 // evaluates to 0 Multiplication operand1 * operand2 The multiplication operator takes two numbers as its operands, and performs the usual arithmetic conversion. 4*3// evaluates to 12 1.2 * 30 // evaluates to 36 20.4 * 6.7 // evaluates to 136.38 Division operand1 / operand2 The division operator also performs the usual arithmetic conversion. However, since JavaScript is loosely typed, this operator does not act exactly as in C, Perl, and other strictly typed programming languages. In those languages, integer division is different
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Graphic web design - Utilizing JavaScript Operators . 93 Chapter7 Utilizing JavaScript

Sunday, May 6th, 2007

Utilizing JavaScript Operators . 93 Chapter7 Utilizing JavaScript Operators Operator Categories JavaScript has many operators, most of them borrowed from C and Perl. The wide variety of operators makes it necessary to divide them into the following categories: . Mathematical operators . String operators . Bitwise operators . Assignment operators . Relational operators . Short-circuit logical operators . More logical operators . Data type operator . Void operator In this chapter we will discuss JavaScript s operators, grouped by the above categories. Operators in each category are divided into two groups: . Unary operators operators that operate on a single operand . Binary operators operators that operate on two operands Mathematical Operators Mathematical operators, also called arithmetic operators, perform basic mathematical operations. Table 7-1 lists the mathematical operators in JavaScript: Table 7-1. Mathematical operators. Syntax Name Type + Addition (plus) Binary Subtraction (minus) Binary * Multiplication (multiply) Binary / Division (divide) Binary % Modulus (modulo) Binary
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

Web hosting top - 92 . Chapter 6 be fine, so the

Saturday, May 5th, 2007

92 . Chapter 6 be fine, so the only possible mistake is with the variables. The exact problem is that the variables are global, not local. Focus on the call to the function, with the value 5. That function then calls itself with the value 3, and after a few more recursive calls, the correct value is returned to s1. Remember that this variable is global. The first function execution (with 5) now calls itself again, this time with the value 4. A few more recursive calls take place, during which the values of s1 and s2 are modified. s1, like s2, is global, so its value is not the same value that was assigned to it in the initial execution of the function (with the argument 5). It holds a value assigned to it by a deeper function call, which is obviously incorrect for the current function. The wrong value of s1 at this point influences the result. The same problem applies to all function calls with a value greater than 4. If you do not understand the cause of the problem, use the following script: var text = “” function getVal(place) { if (place == 1 || place == 2) return 1 s1 = getVal(place 2) var tempS1 = s1 // assign correct s1 value to tempS1 s2 = getVal(place 1) // check if s1 was modified by the preceding statement if (tempS1 != s1) { text += “>>>tThere appears to be an error in the ” text += “current execution of the function (place = ” text += place + “) rthe value of s1 has unexpectedly” text += ” been modified (” + tempS1 + ” => ” + s1 + “)r” } return s1 + s2 } getVal(6) alert(text) Summary In this chapter we focused on advanced function techniques and variable scope. Failing to pay attention to the variable scope concept results in hours of frustrating work trying to find bugs in simple scripts. After attaining a deeper understanding of variables and their scope, you will find it easy to write scripts with many functions, interleaving local and global variables. This chapter also presented some important matters related to parameters, such as referring to a variable number of arguments. Scripting specially designed dialog boxes took advantage of this feature. Another important concept discussed in the chapter is recursion. We found out how to debug recursive algorithms with a formatted table that traces variables.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services