优先使用//注释
二元运算符前后各加一个空格,提高程序的可读性
javascript会自动进行类型的转换
ch4_6.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var firstNumber; var secondNumber; var num1; var num2; var sum; firstNumber = window.prompt("Enter the first number:"); num1 = parseInt(firstNumber); secondNumber = window.prompt("Enter the second number:"); num2 = parseInt(secondNumber); sum = num1 + num2; document.writeln("<h1>The sum is " + sum + "</h1>") //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>
ch4_7.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var name; var now = new Date(); var hour = now.getHours(); name = window.prompt("Enter your name:"); if(hour<12) document.write("<h1>Good morning!"); if(hour >=12) { hour = hour - 12; if( hour > 6 ) document.write("<h1>Good afternoon!"); else document.write("<h1>Good evening!"); } document.writeln( name + ", welcome to javascript programming!</h1>"); //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>
www.deitel.com/javascript Deitel公司的javascript资源中心
ch4_8.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var total; var gradeCounter; var grade; var gradeValue; var average; total = 0; gradeCounter = 1; while( gradeCounter <= 10 ) { grade = window.prompt("Enter the grade:"); gradeValue = parseInt( grade ); total = total + gradeValue; gradeCounter = gradeCounter + 1; } average = total / 10; document.writeln("<h1>Class average is " + average + "</h1>"); //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>
ch4_9.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var total; var gradeCounter; var grade; var gradeValue; var average; total = 0; gradeCounter = 0; grade = window.prompt("Enter the grade:(-1 to quit)","0"); gradeValue = parseInt( grade ); while( gradeValue != -1 ) { grade = window.prompt("Enter the grade:(-1 to quit)","0"); gradeValue = parseInt( grade ); total = total + gradeValue; gradeCounter = gradeCounter + 1; } if( gradeCounter != 0 ) { average = total / gradeCounter; document.writeln("<h1>Class average is " + average + "</h1>"); } else document.writeln("<p>No grades were entered.</p>"); //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>
ch4_10.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var passes = 0; var failures = 0; var student = 1; var result; while( student <= 10 ) { result = window.prompt("Enter result(1=pass,0=fail)","0"); if( result == "1" ) passes = passes + 1; else failures = failures + 1; student = student + 1; } document.writeln("<h1>Examination results:</h1>"); document.writeln("Pass: " + passes + "</br>Fail: " + failures ); if( passes > 8 ) document.writeln("</br>Raised Tuition"); //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>
ch4_11.html
<!DOCTYPE html> <html> <head> <title>Pringting Multiple Lines</title> <script type="text/javascript"> <!-- var c; c = 5; document.writeln("<h3>Postcrement Demo</h3>"); document.writeln(c); document.writeln( "</br>" + (c++) ); document.writeln( "</br>" + c ); c = 5; document.writeln("<h3>precrement Demo</h3>"); document.writeln(c); document.writeln( "</br>" + (++c) ); document.writeln( "</br>" + c ); //--> </script> </head> <body> <p>please refresh or reload this script again!</p> </body> </html>