距離・速度・時間を計算する|JS課題02
解答
HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>距離・速度・時間を計算する</title> <link rel="stylesheet" type="text/css" media="screen,print" href="style.css" /> </head> <body> <script type="text/javascript" src="index.js"></script> </body> </html>
CSS
.answer { color: #f60; font-weight: bold; }
JavaScript
var speed_A = 100; var speed_B = 140; var distance = 560; //時間=距離÷速さより var time_A = distance/speed_A; var time_B = distance/speed_B; var answer; var car; if (time_A>time_B){ answer = (time_A - time_B); answer = answer * 100; answer = Math.round(answer) / 100; car = "A"; } else { answer = time_B - time_A; answer = answer * 100; answer = Math.round(answer) / 100; car = "B"; } document.write("<h2><問題></h2>"); document.write("<p>「車Aの時速は100km」「車Bの時速は140km」</p>"); document.write("<p>東京←→大阪の距離は560km</p>"); document.write("<p>どちらが何時間早く着くか求めなさい</p>"); document.write("<p>※四捨五入の式は考慮しなくてもOK</p>"); document.write("<h2><解答></h2>"); document.write("車" + car + "の方が<span class='answer'>" + answer + "</span>時間早く着く");