単価と個数を入力して、単価が1000円以上ならば単価を2割引して、
それ以外なら1割引したあとに料金を表示する。
| <html> <head> <script language="JavaScript"> <!-- function cal(cost,num){ var total; //単価が1000円以上の処理 if(cost>=1000){ total=cost*0.8*num; window.alert("total price : \\"+total); } //単価が1000円未満の処理 else{ total=cost*0.9*num; window.alert("total price : \\"+total); } //分岐はここまで } //--> </script> </head> <body> 練習 <form name="form1"> Cost:<input type="text" name="txtcost"><br> Number:<input type="text" name="txtnumber"><br> <input type="button" value="計算" onclick="cal(document.form1.txtcost.value,document.form1.txtnumber.value)"> </form> </body> </html> |
| 【解説】 関数「cal」 変数「total」合計額 引数1「cost」単価 引数2「num」個数 基本的なことは前回と大きな違いはないのですが、 計算が入ってくるので複雑になっています。 引数は左側が単価、右側が個数でカンマで区切っています。 色分けをよく見てどのような処理が起きているか確かめてください。 |