280 likes | 453 Views
8 November. Forms and JavaScript. Types of Inputs. Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text) Select List (select from a list) Buttons (click on or off). Select List. Syntax <select name=“name for this list”
E N D
8 November Forms and JavaScript
Types of Inputs • Radio Buttons (select one of a list) • Checkbox (select as many as wanted) • Text inputs (user types text) • Select List (select from a list) • Buttons (click on or off)
Select List • Syntax <select name=“name for this list” size=“number-of-entries-to-show”> <option value=“value-to-be-stored” [selected=”selected”]> Text to be displayed </option> … </select> • What it does • Lets the user select one entry from a list of fixed text • (extension to select multiples) • Optional initial value • Displayed text part of tags • Scroll bar built in • How it does it • Own variable • Value is the selected entry
Buttons • Syntax <input type=“button” value=“name to be displayed” onclick=“what to do when the button is clicked”> • What it does • Lets you perform an action • Need to learn a little Javascript • Lots of different events (e.g., mouseover), but we’ll only worry about “onclick”
Writing a Program • Want to be able to describe a program in human comprehensible terms • Programs translates from those terms to machine comprehensible • Same precision is needed, but do not worry about • where in the computer information is stored • what instructions look like • What you write is referred to as code
Executing a Program • Two types of translators • compiler and interpreter • Compiler creates a program that can be stored and run later without the compiler • Interpreter follows the instructions that are described and is always used when running the program • Writing a program is basically the same in either case
To write a program • Basically two parts: the information and what to do with it • Identify the data that you are going to use • Describe what to do with the data: the algorithm
Example: Calculator • Data • The numbers that are going to be processed • The result • Memory holders • What to do with it • Compute the requested functions • Display the results
Example: Sales • Data • The items for sale: name, cost • The person buying: name, address, credit card information • The actual order: quantity of each item purchased • What to do with it • Compute the cost • Display the current order
General JavaScript Rules • Case-sensitive • Taxrate, TaxRate, and taxrate are all different! • Uses ; to end statements • Similar to a period in English • Unlike people, computers can get confused if you forget it
Creating a Variable • Review: • Computers store values in memory • Refer to the place, not the value • The location is referred to as a variable • A variable’s name is called an identifier • Need to tell computer • The name that you are going to use • What value it starts with • How to interpret the bits: the type of data • Some language require that you tell it • Some deduce it based on what you assign to it • Some permit either (JavaScript)
Types of Data (JavaScript) • Numbers • Integers • Floating point numbers • Strings • Booleans
Representing Integers • Additive system • lllll lllll • Every item represents 1 • Examples of additive systems? • Positional system • Value = face * place • 37 = 3*10 + 7*1
Positional System • Base = number of different values in a position • Base 10 = 10 values: 0-9 • Base 2 = 2 values: 0-1 • Value of each position = power of base • b4 b3 b2 b1 b0
Example in Base 2 1010= 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 1 x 8 + 0 x 4 + 1 x 2 + 0 x 1 8 + 0 +2 + 0 8 + 2 10
Integers • What information is needed? • Sign and magnitude • We use sign-magnitude notation • +1, -1 • Machine arithmetic is harder this way
How Integers are Stored • Two’s complement • We’re not going to look at how it works in detail, but there important characteristics: • Maximum value that can be stored • If checking is not done correctly, positive numbers that are too big will appear to be negative numbers
Example • Assume that we only have 4 bits to work with • Largest positive number is 7 = 0111 • Adding 1 to that number • 0111 • + 1 • 1000 • Turns out that is the representation for the largest negative number (-8)
Why Do You Care? • Properly working program: message that says that the number is too large • Erroneous program: you get a negative number when you expect a positive one
Floating Point Numbers • Scientific notation • 1.452 x 1017 Mantissa exponent • Need to store larger numbers than possible in integers • Use sign magnitude: high order bit = sign • Then store exponent and mantissa • Can be single or double word • Precision of the number
IEEE Floating Point Standard(Single Precision) • The Sign Bit (1 bit) • 0 denotes a positive number; 1 denotes a negative number • The Exponent (8 bits) • Needs to represent both positive and negative exponents • Add a bias of 127. Subtract 127 to see the real exponent • The Mantissa (23 bits) • Implicit leading bit of 1 and the fraction bits • Adjust the value so that the leading bit is 1, then drop it. • Example: 5.00 x 100 = 0.05 x 102 = 5000 x 10-3 • In binary 101 x 100 = 1.01 x 102 • Decimal point is to the left of the stored number (normalized form) • 5 = 1 10000001 01000000000000000000000
Floating Point: Errors • In decimal, 1/3 can’t be represented as a fixed decimal number • Lots of examples in binary as well • Problems occur • Converting between integer and floating point • Displaying results Whittaker, How to Break Software
Text • Need to represent characters • All data in the computer is stored as _____ • Letter a question of interpretation • Also includes some control: new line, tab, … • Not font • Representations • ASCII, EBCDIC, UNICODE, … • ASCII simplest
Valuable Characteristics • Collating sequence • Simple upper/lower case • Support of all languages • Other?
ASCII collating • For collating sequence, where should punctuation be? • General rule: • space < punctuation < number < letter • How to deal with case? • Collating sequence is case insensitive • Need to force to uppercase
Multiple languages • Used to have complete different set of encodings for each language, called code pages • Unicode is fundamentally 2-bytes • Preserves the ASCII values • Doubles the size of files • www.unicode.org
Boolean • Two values: true or false