プリントNo. 9、8ページの例uranai2.htmlです。
※あなたは4月生まれ、相手は10月生まれとして、解答してください。
<html> <head><meta http-equiv="content-type" content="text/html;charset=SHIFT_JIS"> <titile>相性占い</title> <script type="text/javascript"> <!-- // aisyouという名前でひとかたまりの処理をまとめた。 function aisyou(birth1, birth2){ num = Math.abs(birth1 - birth2); if(num == 4 || num == 8){ alert("相性は最高です。"); }else if(num == 2 || num == 6 || num == 10){ alert("相性は最悪です。"); }else{ alert("相性はまあまあです。"); } } //--> </script> </head> <body bgcolor="white"> <h1>インド人もビックリ!</h1> 誕生月で、二人の相性がわかります。ホンマかいな? <p> <script type="text/javascript"> <!-- // メインルーチン ※以下では、あなたは4月生まれ、相手は10月生まれとする yourBirth = prompt("あなたの誕生月を半角英数字で入力してください。", ""); otherBirth = prompt("相手の誕生月を半角英数字で入力してください。", ""); aisyou(yourBirth, otherBirth); // 関数aisyouでyourBirth, otherBirthが引数 //--> </script> </p> </body> </html>上のソースコードをコピー&ペーストしてソースファイルを作成し、WebブラウザでJavaScriptを実行して、動作結果を確かめてみましょう。