Read and Write Client State Cookies • A cookie in Web-speak is a small amount of state data stored permanently or temporarily by the client. • Cookies may be transmitted along with a Web page by the server to the client, which stores them locally. Later, when the client requests the same or related Web page, it passes the relevant cookies back to the server, which can use their values to alter the content it sends back to the client. • For example, when a Web page “remembers” your password from a prior visit. The main reason for cookies is to provide the state information that is missing from the stateless HTTP protocol of the Web.
When you press the OK button, the following text appears in the browser – caused by the document.write() method:
Parsing Numbers • For integers (whole numbers), we use the parseInt() method. • This method requires a string parameter and it returns the integer equivalent. • For example, the following statement asks the user to enter the total questions, parsing this to an integer, and places the result in the variable total. • var total = parseInt (prompt (“Total questions: “, “”)); • For floating-point numbers, we use the parseFloat() method. • For example, if a facilitator assigns partial credit for answers, we could use this method to obtain the answer: • var correct = parseFloat (prompt(“Correct answers: “, “”));
The Rules of Precedence: • Rule 1: Operators are evaluated in the order of their precedence; higher-order operators are evaluated before lower order operators. • Rule 2: Operators of the same precedence are evaluated according to their associatively. Left-associative operators (addition, subtraction, multiplication, division, and modulus) evaluate from left to right. Right-associative operators (assignment) evaluate operators from right to left. • Rule 3: Parentheses are used to override precedence. • Rule 4: Parenthesized expressions are evaluated from the innermost to the outermost set of parenthesis. • Bottom line: USE PARENTHESIS FOR CLARITY AND MAINTAINABILITY!!
Assignment Operators • The equal sign (=) is the main assignment operator. • Other assignment operators are shorthand for certain operators. • x = x + y or use x += y (the shorthand version). Here, we add y = x and store the result back in x. • Other shorthand operators include x -= y (x = x – y), x *= y (x = x * y), and x /= y (x = x/y).