Archive for July, 2007

92 . Chapter 6 be (Domain and web hosting) fine, so the

Tuesday, July 31st, 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.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Functions and Variable Scope . 91 From here (Web hosting services)

Tuesday, July 31st, 2007

Functions and Variable Scope . 91 From here it is effortless to track all the function calls and the values of the variables during the recursion. Use recursive algorithms carefully. Do not create functions that call themselves hundreds of times if there is a simple non-recursive alternative. If you doubt this caution, try calculating the 100th value of the Fibonacci sequence! Variables and Recursive Functions You probably noticed the use of local variables in the previous Fibonacci recursive function. You should regularly use local variables in recursive functions. Make this a habit so you never forget, because it is laborious to detect variable scope-related problems, even when you know the function does not work correctly. This is where variable tracing tables come in handy. Here is the preceding script with the keyword var absent: text=”placetts1tts2ttvalr” text+=”===================================================r” functiongetVal(place) { if(place==1||place==2) return1s1=getVal(place 2) s2=getVal(place 1) text+=place+”t||t”+s1+”t||t” text+=s2+”t||t”+(s1+s2)+”r” returns1+s2} getVal(6) alert(text) As you might expect, the table s content differs: Figure 6-5. A table that helps the scripter detect variable s scope- related problems. An obvious error appears on the sixth line, when the function is called with the value 5. The function returns 4 instead of 5. The seventh line (where place is 6) also reflects an error. The function should return 8, not 5. The first mistake encountered appears to be when the function is invoked with the argument 5. The problem there is the variable s1, because it holds an incorrect value. The recursive algorithm seems to Chapter
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

90 . Chapter 6 Tracing Values in Recursive (Best web site)

Tuesday, July 31st, 2007

90 . Chapter 6 Tracing Values in Recursive Functions Sophisticated recursion algorithms are often difficult to follow. Many values change, some in the way you expect, while others make surprising moves. JavaScript still does not have a decent debugger to help us with that, so we must develop our own tools, using output statements to evaluate expressions and variables. With a few additional statements, it is possible to display the values of the variables and parameters throughout the recursive execution. Here is the getVal() function with a few additions to track values during the recursion: text = “placetts1tts2ttvalr” text += “===================================================r” function getVal(place) { if (place == 1 || place == 2) return 1 var s1 = getVal(place 2) var s2 = getVal(place 1) text += place + “t || t” + s1 + “t || t” text +=s2+”t || t”+(s1+s2)+”r” return s1 + s2 } getVal(6) alert(text) Note that it is legal to not use the value returned by a function (getVal(6) in this case). In the preceding script segment a global variable (text) is used to store the formatted tracing of the execution course. A self-formatted table is created and displayed in an alert box after the execution terminates. The following values are put in each row of the table: . The value of the parameter (place) in the current function call. The assignment to text is done at the end of the function (after the recursive calls to the function), so the first function in the table is the last to be called. . The values of s1, s2, and their sum The exact output of the preceding script segment (the argument handed over to the function is 6) is: Figure6-4. Aformattedtablethathelpsascriptertracevariablesthrougharecursivefunction.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Functions and Variable (Web design company) Scope . 89 Recursive functions

Monday, July 30th, 2007

Functions and Variable Scope . 89 Recursive functions have a few advantages: . Invariably, recursive functions are clearer, simpler, shorter, and easier to understand than their nonrecursive counterparts. . The program directly reflects the abstract solution strategy (algorithm). The recursive factorial function, for example, uses the common mathematical strategy to solve the problem. At first, it may seem that recursive functions are more difficult, but they become easier with practice. Recursion can also be used to compute a value in the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, …). The basic algorithm is: getVal(1) = 1 getVal(2) = 1 getVal(place) = getVal(place 1) + getVal(place 2) Here is a JavaScript function to calculate the value of the sequence in the specified place: function getVal(place) { if (place == 1 || place == 2) return 1 return getVal(place 1) + getVal(place 2) } A call to this function could be: var fibValue7 = getVal(7) // fibValue7 = 13 1, 1, 2, 3, 5, 8, 13, 21, 34, … 1234567 8 9… Note: The bottom row specifies the place; the top row specifies the Fibonacci value at that space. Notice that we once again left out the unnecessary else statement. Some programmers prefer not to use complex expressions and values returned by other functions with the return statement. Instead, they assign those values to variables and then return them: function getVal(place) { 6 if (place == 1 || place == 2) return 1 var s1 = getVal(place 2) var s2 = getVal(place 1) return s1 + s2 } Chapter
You want to have a cheap webhost for your apache application, then check apache web hosting services.

88 . Chapter 6 If (Web hosting faq) the digit is

Monday, July 30th, 2007

88 . Chapter 6 If the digit is between 0 and 9, none of the conditions evaluate to true, so the function returns the same digit it accepts: return dig Recursion A recursive function is usually one that calls itself. It is similar to a loop with or without a terminating condition. Some programming functions lend themselves to recursive algorithms, such as the factorial one. The factorial is a classic recursive algorithm, because it is very obvious. A simple mathematical definition of the factorial operation is: fact(0) = 1 fact(n)=n* fact(n 1) Factorial is commonly represented by the exclamation mark (!). With this algorithm, let s calculate 3!: 1. fact(3)=3* fact(2) 2. fact(2)=2* fact(1) 3. fact(1)=1* fact(0) 4. fact(0) = 1 As you can see, the algorithm works its way down from the given number to zero, so when it gets to fact(0) it must work its way back up to the numbers. We know that fact(0) = 1. fact(1)=1* fact(0)=1*1=1 fact(2)=2* fact(1) =2*1=2 fact(3)=3* fact(2)=3*2=6 We have reached the desired answer using a recursive algorithm. In JavaScript this is: function fact(num) { if (num == 0) return 1 /* else */ return num * fact(num 1) } You can call the function in the following way: var fiveFactorial = fact(5) // 120 Notice that the else statement is commented because it is not necessary. The fact() function satisfies two rules. First, it has an ending point. Second, it simplifies the problem because fact(num 1) is simpler than fact(num). Recursive functions should always follow these two rules.
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Functions and (Graphic web design) Variable Scope . 87 The return

Sunday, July 29th, 2007

Functions and Variable Scope . 87 The return statement returns a value and terminates the function, similar to the way break terminates a loop. Take a look at the following function: function sum(op1, op2) { return op1 + op2 } var answer = sum(3, 5) alert(answer) The value returned by the function is the sum of its arguments. As you can see, the function evaluates to a value. A function may only return one value either a variable, a literal, or any other valid data structure. A function with a return value can also generate an output. When you use the function in an expression, it automatically runs, executing all statements until it returns a value that is used in the expression. The preceding function can also display the sum of its arguments inside the function itself, provided that the alert() statement precedes the return statement (which terminates the function): function sum(op1, op2) { var result = op1 + op2 alert(result) return result } var answer = sum(3, 5) Be sure that the return statement is placed after the statements that should be executed, because it terminates the function and no additional statements of the function are executed afterwards. You can use nested if constructions without else statements, provided that each if statement calls a return statement if the condition is true. Eliminating the else statements does not affect the efficiency of the function, because the function terminates immediately after a true condition is met. The following function converts hexadecimal digits (not numbers) to their equivalent decimal values: 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 needfor else statements } Chapter
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

86 . (Web design seattle) Chapter 6 . Caution: Be aware

Sunday, July 29th, 2007

86 . Chapter 6 . Caution: Be aware that the preceding checkDefault() function is not compatible with Netscape Navigator 2.0x or Microsoft Internet Explorer 3.0x, because they do not support the typeof operator. However, this is not a problem with the most recent versions of these two browsers. Use the arguments array to simulate C++ s default arguments feature. The previous drawLine() function would then be as follows: function drawLine(character, numOfChars) { args = drawLine.arguments if (args.length < 1) character = "*" if (args.length < 2) numOfChars = 30 for (vari=0;i< numOfChars; i++) { document.write(character) } } If the function receives no arguments, the value of character is set to the default * . If the function received one argument or less, the parameter numOfChars defaults to 30. Because this function uses the arguments array, named parameters are not necessary you can use the array instead as in the following function definition: function drawLine() { args = drawLine.arguments if (args.length < 1) args[0] = "*" if (args.length < 2) args[1] = 30 for (vari=0;i< args[1]; i++) { document.write(args[0]) } } Returning a Value A function can return a value, so it is possible to use a function call as a valid JavaScript value. Use this feature to create general functions for usage by other scripts. For example, you can use a function that accepts a number in one base and returns it in a different representation. This function can be used in creating a base-conversion table, a dialog box, or additional calculations. Use the following syntax to return a value from within a function: return value
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web site hosting - Functions and Variable Scope . 85 Creating Functions

Saturday, July 28th, 2007

Functions and Variable Scope . 85 Creating Functions with Default Arguments JavaScript does not support functions with default arguments. In C++, these are the values supplied to the parameters in a function s prototype. Calling the following function at the beginning of a function is a simple workaround you can use in JavaScript: functioncheckDefault(parameter,defaultValue) { if(typeofparameter==”undefined”) returndefaultValue/*else*/ returnparameter} The appropriate call to this function is: parameterName=checkDefault(parameterName,defaultValue) The parameter is assigned its default value only if the function is called without specifying the required argument. The following function uses this technique to print a row of a given character; an asterisk is the default character, and 30 characters is the default length: functioncheckDefault(parameter,defaultValue) { if(typeofparameter==”undefined”) returndefaultValue/*else*/ returnparameter} functiondrawLine(character,numOfChars) { character=checkDefault(character,”*”) numOfChars=checkDefault(numOfChars,30) for(vari=0;iIn case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

84 . Chapter 6 func(”a”) func(”b”) func() func(”c”) (Business web site)

Saturday, July 28th, 2007

84 . Chapter 6 func(”a”) func(”b”) func() func(”c”) The output of this script is: a b b (!) c The only surprising line is the third one. The argument printed is the one passed to the function during the previous call. The first argument of the current call cannot be printed because there is no such argument. In Internet Explorer this will print as undefined . At times, you will receive unexpected results from the func.arguments.length property, as well as from the elements of the array itself. Note, also, that you may only refer to the arguments array from within the function. The scope of the arguments object s properties is the current function, so it can only be used inside a function. You can use loop statements to print a list of arguments handed to a function: function createList() { var result = “” for (var i = 0; i < createList.arguments.length; ++i) { result += createList.arguments[i] + "r" } alert(result) } Here is a simple function call: createList("C", "H", "U", "C", "K ) When invoked with the preceding statement, the function generates the following dialog box: Figure 6-3. Each line displayed is a distinct argument handed to the createList() function.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Functions and Variable Scope . 83 Note: (Web design portfolio)

Friday, July 27th, 2007

Functions and Variable Scope . 83 Note: Arguments are the specific values you assign to parameters. These terms are sometimes considered exchangeable, even by advanced programmers. It is sometimes difficult to distinguish between them, so you might disagree with some of the terms in this chapter. Using the Argument Array JavaScript supports functions that accept a variable number of arguments. The first argument is functionName.arguments[0], the second one is functionName.arguments[1], the third one is functionName.arguments[2], and so on. The number of arguments handed to the function is stored in the length property of the arguments object: functionName.arguments.length The following script demonstrates this concept: functioncalc() { document.write(”Thefirstargumentis”,calc.arguments[0],”
“) document.write(”Thefourthargumentis”,calc.arguments[3],”
“) document.write(”Thereare”,calc.arguments.length,”arguments
“) } varcompany=”Yahoo” calc(2,999,”internet”,company,0.0) The script s output is: The first argument is 2 The fourth argument is Yahoo There are 5 arguments . Caution: Netscape Navigator allows you to use arguments rather than functionName.arguments. However, Microsoft s Internet Explorer 3.0 generates an error when you implement that shortcut. If you want to be on the safe side, you can use the following statement at the beginning of the function: var args = functionName.arguments You can then use args in the regular way (args[0], args.length, etc.). The ability to refer to the arguments passed to the function during the last call of the function often trips up JavaScript scripters. For example, consider the following script: function func() { document.write(func.arguments[0] + “
“) } Chapter
You want to have a cheap webhost for your apache application, then check apache web hosting services.