chapter 4 - Sixth Edition JavaScript

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is a breakpoint? How do you set a breakpoint?

A breakpoint is a designation added to a specific statement in a program that causes program execution to pause when it reaches that statement. To set a breakpoint, you click the line number for the line where program execution should stop.

Explain two different ways that a text editor specialized for web development can help you in preventing errors and debugging code?

A text editor automatically highlights syntax errors which draws your attention to any errors it identifies before and after fully typing the code in.

Which of the following for statements is logically incorrect? A. for (var count = 10; count <= 0; count++) { document.write(count);} B. for (var count = 0; count <= 10; count++) { document.write(count);} C. for (var count = 10; count >= 0; count--) { document.write(count);} D. for (var count = 5; count >= 0; count--) { document.write(count);}

A. for (var count = 10; count <= 0; count++) { document.write(count);}

Which of the following pieces of information is passed as an argument from a throw statement to a catch statement? A. Error number B. Error message C. Line number D. URL

B. Error message

Which command executes all the statements in the next function in browser debugging tools? A. Step out B. Step over C. Step D. Step in/into

B. Step Over

In ___, some features are removed from the JavaScript language, while other features require more stringent syntax? A. Exception handling B. Strict mode C. Debugging tools D. Debugging mode

B. Strict mode

Which of the following statements causes a syntax error? A. var firstName = ""; B. document.write(Available points: " + availPoints); C. readyState == true; D. "use strict";

B. document.write(Available points: " + availPoints);

What type of error occurs when the interpreter fails to recognize code? A. debugging B. syntax C. run-time D. logic

B. syntax

Which of the following modes temporarily suspends, or pauses, program execution so that you can monitor values and trace program execution? A. Suspend B. Step C. Break D. Continue

C. Break

The watch list in browser debugging tools lets you monitor the values of a(n) _____ during program execution? A. function B. Exception handler C. Expression D. statement

C. Expression

________ errors are problems in the design of a program that prevent it from running as you anticipate? A. Application B. Syntax C. Logic D. Run-time

C. Logic

After you throw an error, you use a(n) _____ statement to handle the error? A. Try B. Throw C. Catch D. finally

C. catch

When is commenting out code useful in debugging?

Commenting out code is useful when you have narrowed down an error to a specific part of the code but don't know exactly which line or lines contain the error.

The _____ is the order list maintained by a javaScript processor containing all the procedures, such as function, methods, ore event handlers, that have been called but have not yet finished processing? A. Variables list B. Watch list C. Strict mode D. Call stack

D. Call Stack

Which of the following exception handling code executes regardless of weather its associated try block throws an exception? A. throw "Please enter your last name."; B. catch(1NameError) { return false; } C. catch (1NameError) { window.alert (1NameError) return false; } D. Finally { 1NameValid = true; }

D. Finally { 1NameValid = true; }

When a JavaScript interpreter encounters a problem while program is executing, that problem is called a(n) _______ error? A. Application B. Syntax C. Logic D. Run-time

D. Run-time

Which of the following statements writes the value of the selection variable to the console? A. console.log("selection"); B. document.console("selection"); C. console.alert(selection); D. console.log(selection);

D. console.log(selection);

What statement can you add to your code to effectively serve the same role as a breakpoint? A. break; B. breakpoint; C. debug; D. debugger;

D. debugger;

When and why should you use exception handling with your JavaScript programs?

Exception handling helps with specific errors while coding a function.

Explain how coding in strict mode can help you write better code.

For debugging purposes, it can be useful to develop and test all your code using strict mode. This not only helps you notice issues in your code that may be causing problems, but it also helps you strengthen your coding techniques to use current best practices, rather than relying on some parts of the language that are still technically part of the specification but whose use is not generally recommended

Suppose your browser console lists a single error, which you find and fix. Why is it important to save your work and reload the page in the browser?

Generally when a browser encounters a syntax error, the browser is unable to process any JavaScript code that follows the error. Because there are a number of possible reasons for—and ways of fixing—any given error, the processor can no longer reliably parse the code that comes after the error. Therefore, saving and reloading a page after fixing an error may uncover additional errors.

Explain how to debug code by commenting it out?

Identify lines that you assume might have an error and comment it out

Where can you find error messages in a browser?

In the browser's console.

Explain the differences between stepping in (or into), stepping over, and stepping out.

Stepping in or stepping into, executes an individual line of code and then pauses until you instruct the debugger to continue. This feature gives you an opportunity to evaluate program flow and structure as code is being executed. As you use the Step Into button to move through code, the debuggers stop at each line within every function of the JavaScript program. However, when stepping through a program to trace a logical error, it is convenient to be able to skip functions that you know are functioning correctly. The second option, known as stepping over, allows you to skip function calls. The program still executes each function that you step over, but it appears in each debugger as if a single statement executes. The final option, stepping out, executes all remaining code in the current function. If the current function was called from another function, all remaining code in the current function executes and the debugger stops at the next statement in the calling function.

Explain what strict mode is, how to implement it, and how it's useful in reducing code errors?

Strict mode removes certain features, and needs more complex syntax for others. "use strict" informs the user of errors and to reduce problems in the future teaches the user of good coding practice.

Explain the difference between syntax errors, run-time errors, and logic errors. Provide an example of each.

Syntax errors occur when the interpreter fails to recognize code. For example, if a programmer attempts to use a method that does not exist or omits a method's closing parenthesis, the scripting engine generates a syntax error. A run-time error occurs when the JavaScript interpreter encounters a problem while a program is executing. Run-time errors differ from syntax errors in that they do not necessarily represent JavaScript language errors. Instead, run-time errors occur when the interpreter encounters code that it cannot execute. For example, consider the statement createRecommendation();, which calls a custom JavaScript function. This statement does not generate a syntax error, because it is legal (and usually necessary) to create and then call custom functions in a JavaScript program. However, if your program includes the call statement but does not include code that creates the function in the first place, your program generates a run-time error. The error occurs when the interpreter attempts to call the function and is unable to find it. A logic error is a flaw in a program's design that prevents the program from running as you anticipate. One example of a logic error is the creation of an infinite loop, in which a loop statement never ends because its conditional expression is never updated or is never false.

What is the call stack? How do you use it in debugging?

The call stack is the ordered list maintained by a JavaScript processor containing all the procedures, such as functions, methods, or event handlers, that have been called but have not yet finished processing. Each time a program calls a procedure, the procedure is added to the top of the call stack and then removed after it finishes executing. The ability to view a list showing the contents of the call stack is very useful when tracing logic errors in large programs with multiple functions. Viewing the call stack, along with using tracing commands, makes it easier to locate the specific function causing the problem.

How can code editors designed for web development help you in identifying errors in your HTML?

These editors automatically highlight syntax errors in HTML, CSS, and JavaScript code as you type. This means that rather than needing to scrutinize each line of code, you can count on the editor to visually draw your attention to any errors it identifies— often even before you test your code.

How do you reference a previously generated error message in a catch() statement?

Whatever argument name you specify when creating the catch statement is the variable name you use within statements to refer to the text of the thrown exception.

When is it necessary to include exception handling in your code? Give an example.

You use exception handling to test any type of input or functionality that is external to a program. The main reason for using exception handling in JavaScript is to evaluate user input.

What statement would you use to log the text "itemTotal: " plus the value of the itemTotal variable to the console?

console.log("itemTotal: " + itemTotal);

What statement in the code to handle an exception do you use to specify an error message?

throw

What code would you enter on the command line to declare a variable named cost with a value of 75, and then log to the console the result of multiplying the cost variable by 1.2?

var cost = 75; cost*1.2;

What are the two different statements you can add to your code to provide you with additional information while you're debugging?

window.alert() console.log()

What is the advantage of tracing errors using the window.alert() method? What is the advantage of using the console.log() method instead?

window.alert() method technique can monitor a person change things during program execution. console.log() method has syntax where value can be stringed literally


Ensembles d'études connexes

Electroticity Chapter 12 & 13 (Mid-Term)

View Set

Project Management Exam 2 (Ch 8-11)

View Set

Kingfisher Security+ SYO-501 Chapter 2: Computer System Security Part 1

View Set

Biological Anthropology 196 Part 1

View Set

Political Science T/F questions (ch.1-3)

View Set

Wordly Wise Book 9 Lesson 10 (definitions)

View Set