HTML and CSS Reference
In-Depth Information
Figure 10-30
Results of a logical error
username s and
e-mail ser v er names
are displa y ed in the
wrong or d er
Although the browser did not report any mistakes, this is obviously not the way Kate
wants e-mail addresses displayed!
Problem Solving: Fixing Common Programming Mistakes
When you begin writing JavaScript programs, you will invariably encounter mistakes in
your code. Some common sources of programming error include:
• Misspelling a variable name : For example, if you named a variable ListPrice , then
misspellings or incorrect capitalization—such as listprice , ListPrices , or list_
price —will result in the program failing to run correctly.
• Mismatched parentheses or braces : The following code results in an error because the
function lacks the closing brace:
functionƒArea(width,ƒheight)ƒ{
ƒƒƒvarƒsizeƒ=ƒwidth*height;
• Mismatched quotes : If you neglect the closing quotes around a text string, JavaScript
treats the text string as an object or variable, resulting in an error. The following code
results in an error because the closing double quote is missing from the firstName
variable:
varƒfirstNameƒ=ƒ“Sean';
varƒlastNameƒ=ƒ“Lee”;
document.write(firstName+”ƒ“ƒ+ƒlastName);
• Missing quotes : When you combine several text strings using the + symbol, you might
neglect to quote all text strings. For example, the following code generates an error
because of the missing quotes around the <brƒ/> tag:
document.write(“MidWestƒStudentƒUnion”ƒ+ƒ<brƒ/>);
As you become more experienced in writing JavaScript code, you'll be able to quickly
spot these types of errors, making it easier for you to debug your programs.
Search WWH ::




Custom Search