Topics Introduction Simple Program: Printing a Line of Text in a Web Page Obtaining User Input with Prompt Dialogs Dynamic Welcome Page Adding Integers Memory Concepts Arithmetic Decision Making: Equality and Relational Operators
Simple Program: Printing a Line of Text in a Web Page Inline scripting –Written in the of a document – tag Indicate that the text is part of a script type attribute –Specifies the type of file and the scripting language use write method –Write a text in the document 5
Simple Program: Printing a Line of Text in a Web Page Unlike writeln, write does not position the output cursor in the XHTML document at the beginning of the next line after writing its argument. 9
Simple Program: Printing a Line of Text in a Web Page Display the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Using document.write document.write("1 "); document.write("2 "); document.write("3 "); document.write("4 "); Using document.writeln document.writeln("1"); document.writeln("2"); document.writeln("3"); document.writeln("4"); 10 Spaces are needed
Dynamic Welcome Page 19 Fig. 7.7Prompt dialog displayed by the window object’s prompt method. This is the prompt to the user. This is the default value that appears when the dialog opens. This is the text field in which the user types the value. When the user clicks OK, the value typed by the user is returned to the program as a string.
Adding Integers Prompt user for two integers and calculate the sum (Fig. 7.8) NaN (not a number) parseInt –Converts its string argument to an integer 20
Sample Program 1 Write a script that gets from the user the radius of a circle and outputs XHTML text that displays the circle’s diameter, circumference and area. Use the constant value 3.14159 for PI. [Note: You may also use the predefined constant Math.PI for the value of PI. This constant is more precise than the value 3.14159. The Math object is defined by Java-Script and provides many common mathematical capabilities.] Use the following formulas (r is the radius): diameter = 2r, circumference = 2PIr, area = PIr2. INPUT: radius of a circle OUTPUT: circle’s diameter, circumference and area RELEVANT FORMULA: PI= 3.14159 diameter = 2r, circumference = 2PIr, area = PIr2 37