Transcript Document

JavaScript:
The First Parts
Part Two
Douglas Crockford
Yahoo! Inc.
Learn to Program
• Values
• Functions
• Variables
• Recursion
• Expressions
• Arrays
• Branching
• Objects
• Loops
• Trees
Values
• Booleans
• Numbers
• Strings
• null
• undefined
Boolean
• true
• false
• Named for George Boole
• &&
and
• ||
or
•!
not
Numbers
• Modern computers like to represent
numbers in binary (base 2).
27
26
25
24
23
22
21
20
128 64
32
16
8
4
2
1
Numbers
• Binary numbers usually come in fixed
sizes, such as 8, 16, 32, 64.
• The number of bits determines the range of
the values.
Number of bits
Maximum value
8
255
16
32
64
65535 4294967295 18446744073709551615
Numbers
• JavaScript provides only one number type.
• It is a 64-bit binary floating-point number.
...
211
210
29
28
27
26
25
24
23
22
21
20
2-1
2-2
2-3
2-4
2-5
2-6
2-7
2-8
mantissa or significand
• Floating point numbers can cover an
enormous range.
• 5E-324 to 1.7976931348623157E308
2-9
2-10
2-11
...
But it comes at a cost.
• Most decimal fractions cannot be
represented accurately.
...
211
210
29
28
27
26
25
24
23
22
21
20
2-1
2-2
2-3
2-4
2-5
2-6
2-7
2-8
2-9
mantissa or significand
• A decimal fraction requires an infinite
number of bits, but mantissas are finite.
2-10
2-11
...
But it comes at a cost.
• Most decimal fractions cannot be
represented accurately.
...
211
210
29
28
27
26
25
24
23
22
21
20
2-1
2-2
2-3
2-4
2-5
2-6
2-7
a
2-8
2-9
b
c
• The associative and distributive laws do
not hold.
(a + b) + c ≠ a + (b + c)
2-10
2-11
...
Integers that fit entirely within
the mantissa are safe.
Write a program to compute
safe_max
• There are integers such that adding 1 will
not result in a larger integer. Such integers
are unsafe integers.
Write a program to compute
safe_max
• There are integers such that adding 1 will
not result in a larger integer. Such integers
are unsafe integers.
• We want to find the largest positive safe
integer. Adding 1 to it produces an unsafe
integer.
Write a program to compute
safe_max
• There are integers such that adding 1 will
not result in a larger integer. Such integers
are unsafe integers.
• We want to find the largest positive safe
integer. Adding 1 to it produces an unsafe
integer.
• This will require a loop and a variable, in
which we produce and test candidate
integers.
First Attempt
var safe_max = function () {
var integer = 1;
while (integer + 1 !== integer) {
integer = integer + 1;
}
return integer;
};
• Go to the JSMVHS group
• Go to files
• Get template1.txt
• Start a text editor, such as Crimson Editor.
• Edit the template, save it as safe_max.html
<html><head>
// The safe_max function, as written, is too slow.
<title>template1.html</title>
// It will not be allowed to finished.
<style>
// It needs to be corrected.
div[id] {
border: 1px solid black;
margin: 1em;
var safe_max = function () {
padding: 1em;
var integer = 1;
}
while (integer + 1 !== integer) {
</style></head>
integer = integer + 1;
<body>
}
<script
return integer;
src="http://www.ADsafe.org/adsafe.js"></script>
};
<div id="MAX_">
dom.q('#MAX_RESULT').value(safe_max());
<p>Compute the largest safe integer.</p>
});
<p id="MAX_RESULT"></p>
</script>
<script>
</div>
"use strict";
ADSAFE.go("MAX_", function (dom, lib) {
</body></html>
First Attempt
var safe_max = function () {
var integer = 1;
while (integer + 1 !== integer) {
integer = integer + 1;
}
return integer;
};
Second Attempt
var safe_max = function () {
var integer = 1;
while (integer + 1 !== integer) {
integer = integer * 2;
}
return integer;
};
9007199254740992
• nine quadrillion seven trillion one hundred
ninety nine billion two hundred fifty four
million seven hundred forty thousand nine
hundred ninety two
• Integer operations are exact if the
safe_max limit is never exceeded.
• If safe_max is exceeded, this might be
false:
(a + 1) - 1 === a
eps
• eps is that smallest number which, when
added to 1 produces a number that is
larger than 1.
...
211
210
29
28
27
26
25
24
23
22
21
20
2-1
2-2
2-3
2-4
2-5
2-6
2-7
mantissa or significand
• eps is short for epsilon.
2-8
2-9
2-10
2-11
...
First attempt
var compute_eps = function () {
var eps = 1;
while (1 + eps !== 1) {
eps = eps / 2;
}
return eps;
};
var eps = compute_eps();
Test the result
var test_eps = function (e) {
if (1 + e <= 1) {
alert("Test failed.");
}
};
test_eps(eps);
Why did it fail?
Second attempt
var compute_eps = function () {
var eps, next = 1;
while (1 + next !== 1) {
eps = next;
next = next / 2;
}
return eps;
};
var eps = compute_eps();
Learn to Program
• Values
• Functions
• Variables
• Recursion
• Expressions
• Arrays
• Branching
• Objects
• Loops
• Trees
Array
• An array is a collection of values.
• Each value is given a number.
• The first value is 0, not 1.
• Create an array with the [ ] operator.
• Access an element of an array with the [ ]
operator.
http://jsmvhs.crockford.com/yellowbox.html
• data = [17, 19, 23, 29];
• data.length
• data[0]
• data[1]
• data[2] = 0
• data[data[2]]
• data[3]
• data[4]
• data[4] = 31
• data.length
Looping
• The for statement can be used to visit
every element of an array.
var alert_array = function (array) {
var i;
for (i = 0; i < array.length; i = i + 1) {
alert(array[i]);
}
};
alert_array([36, 42, 10]);
Assignment 2
• Write an average_array function that
takes an array of numbers and returns the
average of its elements.
• Use template1 to package it in an HTML
page.
• Add it to your assignments folder.