【AS02課題】jsバージョン

問題

「車Aの時速は100km」「車Bの時速は140km」
東京←→大阪の距離は560km
どちらが何時間早く着くか求めなさい?
※四捨五入の式は考慮しなくてもOK

解答


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="content-script-type" content="text/javascript" >
<title>無題ドキュメント</title>
</head>

<body>

Aのスピード:<input type="text" name="sppedA" id="sppedA"><br>
Bのスピード:<input type="text" name="sppedB" id="sppedB"><br>
走行距離:<input type="text" name="distance" id="distance"><br>


<input type="button" name="as" value="計算" onclick="calc()" >

<br>
解答:<span id="output"></span>



</body>
<script type="text/javascript">
<!--

function calc(){
		var timeA=calcTime(document.getElementById("sppedA").value,document.getElementById("distance").value);
		var timeB=calcTime(document.getElementById("sppedB").value,document.getElementById("distance").value);
		var obj=document.getElementById("output");
		var margin=0;

		if(timeA>timeB){
				margin=timeA-timeB;
				obj.innerHTML="BがAより"+margin+"時間早く着きました";
		}else{
				margin=timeB-timeA;
				obj.innerHTML="AがBより"+margin+"時間早く着きました";
		}

}

function calcTime(speed,disance){
		return disance/speed;
}

// -->
</script>


</html>