3月1日授業内容 javascript(変数var,alert,console.log,document,write,prompt,parseInt,変数名.toFixed(2))


f:id:paris1204:20160301231921p:plain
f:id:paris1204:20160301231957p:plain

<script>
//javascriptの練習
//初期値
x=1500;
tax=0.08;

//処理(価格1500円の商品の税込み金額を求める)(あまり数字を入れないほうが良い)
var total=x+x*tax;//var=variable


//出力の記述3種類

document.write(total);//ブラウザ画面表示       .で繋ぐことをドットシンタックスすいう
alert( total);//アラート表示
console.log(total);//コンソールで表示される

</script>



f:id:paris1204:20160301232455p:plain

<script>
//javascriptの練習
//初期値(初めて出てきたときはvarの変数宣言をする)
var x=1500;
var tax=0.08;

//処理(価格1500円の商品の税込み金額を求める)(あまり数字を入れないほうが良い)
var total=x+x*tax;//var=variable


//出力の記述3種類

document.write('<h1>価格1500円の商品の税込み金額は、');
document.write(total);
document.write('円です。</h1>');

</script>

f:id:paris1204:20160301230932p:plain
f:id:paris1204:20160301230943p:plain

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>平成を西暦に変換する</title>
</head>

<body>
<script>
var heisei;
var fullYear;
var msg;

heisei=prompt('平成年号を半角で入力してください', 2);
heisei=parseInt(heisei)
 fullYear = heisei + 1988;
 console.log(fullYear);//検証画面で結果を確認するために入れる。
 //文字列と数値を並べて出力する場合には、「+」連結演算子を利用。
 
 msg='平成' + heisei + '年は' +fullYear + '年です。';
 document.write('<h1>' + msg + '</h1>');//document.write('<h1>');document.write(msg);document.write('</h1>')を1つにしたもの;
 
 
 
</script>

</body>
</html>


f:id:paris1204:20160301231257p:plain
f:id:paris1204:20160301231301p:plain
f:id:paris1204:20160301231303p:plain

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>標準体重を求めるプログラム</title>
</head>

<body>
<script>
/*
*標準体重計算プログラム
*最終修正日:2016.03.01
*/

var height;//身長
var weight;//体重
var man;//男性である

//男性か女性かの選択
man=confirm('あなたは男性ですか?')

//身長を代入する
height=prompt('身長をcmで半角数字で入力してください',170);
height=parseInt(height);//parseIntを入れると表示を整数化できる。

//計算を行う
if(man){
	weight=(height - 80)*0.7;
}
else{weight=(height - 70)*0.6;
}

//結果を表示する
document.write('<h1>')
document.write('身長が' + height +'cmの人の標準体重は');
document.write(weight + 'kgです。');
document.write('</h1>')


</script>
</body>
</html>


f:id:paris1204:20160301231426p:plain
f:id:paris1204:20160301231430p:plain

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>消費税計算</title>
</head>

<body>
<script>

//	初期値
var x;
var tax=0.08;




//処理
x=prompt('価格を半角数字で入力してください',1000);//ダイアログボックスで値を入力してもらう役割。
x=parseInt(x);//入力された文字列を、整列化する。
//promptで入力したものは数値であっても、文字列+数字の場合は文字列表示になってしまうのでそれを避けるためにparseIntを入れる

var total=x+x*tax;//文字*文字のときは数値に変換される



//出力
document.write('<h1>価格' + x + '円の商品の税込み金額は、' + total.toFixed(0) + '円です。');
//document.write('<h1>価格' + x + '円の商品の税込み金額は、' );
//document.write(total.toFixed(0));//小数点の表示の制限をかけられる
//document.write('円です。');
</script>


</body>
</html>