平方根の計算

左側のボックスに「0以上の数字(小数でもOK!)」を入れて「=」をクリックして下さい。
右側のボックスに計算結果が表示されます。

の平方根

【Source Code】
<script type="text/javascript">
<!--
function keisan(form)
{ if (form.num.value == "")
  { window.alert("データを入力して下さい♪");
    return;
  }
  if (isNaN(form.num.value) == true)
  { window.alert("数値を入力して下さい♪");
    return;
  }
  if (eval(form.num.value) < 0)
  { window.alert("0以上の数を入力して下さい♪");
    return;
  }
  form.root.value = Math.sqrt(form.num.value);
}
// -->
</script>

</head>
<body>
<h2>平方根の計算</h2>

<span class="s4">
左側のボックスに「0以上の数字(小数でもOK!)」を入れて「=」をクリックして下さい。<br>
右側のボックスに計算結果が表示されます。<br>
</span><br>

<form>
<input type="text" name="num" value="" size=20>
の平方根
<input type="button" nama="ans" value="=" onClick="keisan(this.form)">
<input type="text" name="root" value="" size=30>
</form>

データをタイプ入力する form です。
button をクリックすると keisan(this.form) が呼ばれます。
this.form は keisan 関数に渡すパラメータで、これを通じて form の領域を参照します。
計算結果は name="root" の text box に表示されます。
<center>
<form>
<input type="text" name="num" value="" size=20>
の平方根
<input type="button" nama="ans" value="=" onClick="keisan(this.form)">
<input type="text" name="root" value="" size=30>
</form>
</center>

平方根を計算する関数です。
form.num にタイプされたデータを取得して Math.sqrt で計算します。
計算結果は、引数で渡された form.root.value に直接格納します。
このプログラムは、石取りゲームのように新しいページに遷移することはありません。
<script type="text/javascript">
<!--
function keisan(form)
{ if (form.num.value == "")
  { window.alert("データを入力して下さい♪");
    return;
  }
  if (isNaN(form.num.value) == true)
  { window.alert("数値を入力して下さい♪");
    return;
  }
  if (eval(form.num.value) < 0)
  { window.alert("0以上の数を入力して下さい♪");
    return;
  }
  form.root.value = Math.sqrt(form.num.value);
}
// -->
</script>

前田稔の超初心者のプログラム入門
超初心者のプログラム入門(JavaScript)