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

 


 ПОДПИСКА

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




Previous Table of Contents Next

Notice that the function includes three loops. The first one prints the header of each column (e.g., base 2, base 4, base 6, ...). The second one deals with the vertical dimension of the table. The third one is nested in the second one, and it deals with the horizontal dimension, basically building the rows of the table. This loop terminates after it has constructed the entire row, whereas the second one terminates when the entire table is finished.

The drawTable() function lacks efficiency. For example, it converts numbers from decimal notation to decimal notation. You may want to rewrite either this function or the conversion ones.

calcNum()

// Converts individual numbers, until the
// user selects cancel on the first
// prompt of the loop
/////////////////////////////////////////

function calcNum() {
 while(1) {
  var number = prompt("Enter a number in any base:", 0)
  if (number == null)
   break
  var base1 = prompt("Enter its base:", 10)
  if (base1 == null)
   continue
  base1 = parseInt(base1)
  var base2 = prompt("Enter the desired base:", 16)
  if (base2 == null)
   continue
  base2 = parseInt(base2)
  var outputString = number + " (base " + base1 + ") = "
  outputString += convert(number, base1, base2)
  outputString += " (base " + base2 + ")"
  alert(outputString)
 }
}

The calcNum() function asks the user for the number he or she wants to convert, its base, and the desired target base. It demonstrates many control structures. The entire function is actually a single loop. The break statement converts the while(1) loop to a finite one. The break statement is executed if the user clicks cancel on the first prompt box (“Enter a number ...”). Cancellation of other prompt boxes invokes a continue command, sending JavaScript directly to the beginning of the loop. Since any textbox in JavaScript (in a prompt box or a form) is evaluated to a string, all input values need to be converted to numbers (via parseInt()), even if the content appears to be numeric. The function then creates a message including the original number-base pair, together with the new one. The message is displayed in an alert box.

mainInput()

// Asks user for conversion device
// (T-table, V-individual values)
/////////////////////////////////////////

function mainInput() {
 var message = "Enter (T) to create a table or (V) to "
 message += "calculate a value"
 var chosenDevice = prompt(message, "T")
 if (chosenDevice == "T" || chosenDevice == "t")
  getTableAttributes()
 else
  if (chosenDevice == "V" || chosenDevice == "v")
   calcNum()
  else
   alert("Goodbye!")
}

This is the main function. It asks the user if he or she wants to create a table or convert a single entry. According to the user’s response, an appropriate function is called. It is basically built of a nested if - else statement. The logical OR operator is used to test for both uppercase or lowercase input.

Global Statements

The only global statement is the call to the function mainInput().

Output


Figure 10-6.  The function displays the main option via an alert box.

Here are a few screens generated by the base converter script, demonstrating output via HTML and dialog boxes:


Figure 10-7.  A base conversion table made by “pure” JavaScript output.


Figure 10-8.  The user enters a number in any base.


Figure 10-9.  The final result.

Summary

In this chapter, we have explained in detail two complete examples: the Weight and Height Calculator, and the Base Converter. These scripts are a good summary of the first part of the book, and you should take the time to comprehend them in depth. They demonstrate the basic features of the language such as variables, variable scopes, conditional and loop structures, functions, alert boxes, forms, prompts, output techniques, data type conversion, and more. The scripts in the remainder of the book are much more complicated, so make sure your knowledge base is as broad as these scripts entail.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us