JavaScriptのプログラム実習
プログラムリスト
- 和暦を西暦に変換するプログラム
- 偶数・奇数の判別プログラム
- 標準体重を求めるプログラム
- 男性か女性かの入力をして標準体重を求めるプログラム
- 季節を求めるプログラム
和暦を西暦に変換するプログラム
<body>
<script type="text/javascript">
var heisei;
var fullyear;
var message;
heisei = prompt('平成の年号を入力してください。','例:28');
fullyear = parseInt(heisei) + 1988;
message = '平成' + heisei + '年は' + '西暦' + fullyear + '年です。';
document.write('<h1>');
document.write(message);
document.write('</h1>');
document.write('<h2>'+message+'<h2>');
document.write('<h3>'+message+'<h3>');
document.write('<p>'+message+'<p>');
</script>
</body>
偶数か奇数かを判別するプログラム
<body>
<script>
var i = prompt('1から100までの整数を入力してください。','例)1 - 100');
var j = parseInt(i);
console.log(i);
console.log(j);
if ( j === 0 ) {
document.write('入力値は0です。1から100までの整数を入力してください。');
} else if ( j%2 === 0 ) {
document.write('入力値'+i+'は偶数です。');
} else if ( j%2 === 1) {
document.write('入力値'+i+'は奇数です。');
} else {
document.write('半角の整数ではありません。1から100までの整数を入力してください。');
}
</script>
</body>
i = parseInt( str, int )
i:変換された数値
str:変換したい文字列
int:基数 intを省略すると10進数
2 2進数
8 8進数
10 10進数
16 16進数
上記プログラムを関数化
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>偶数・奇数の判別プログラムを関数化</title>
<script>
function evenOdd() {
上記の「偶数・奇数の判別プログラム」をここに移す
}
</script>
</head>
<body>
<h1>偶数奇数を判別するプログラム</h1>
<p>判別ボタンを押して、入力窓に1から100までの半角を入力すると、その数字が偶数か奇数かの結果を表示します。</p>
<p><button onclick="evenOdd()">判別する</button></p>
</body>
<script>
var i = prompt('1から100までの整数を入力してください。','例)1 - 100');
var j = parseInt(i);
console.log(i);
console.log(j);
if ( j === 0 ) {
document.write('入力値は0です。1から100までの整数を入力してください。');
} else if ( j%2 === 0 ) {
document.write('入力値'+i+'は偶数です。');
} else if ( j%2 === 1) {
document.write('入力値'+i+'は奇数です。');
} else {
document.write('半角の整数ではありません。1から100までの整数を入力してください。');
}
</script>
標準体重を求めるプログラム
<style>
button {
font-size: 24px;
cursor: pointer;
}
</style>
<script>
function bmi() {
var height;
var weight;
height = prompt('あなたの身長を入力してください。','例) 170');
console.log(height);
height = parseInt(height);
console.log(height);
weight = (height - 100) * 0.9;
console.log(weight);
document.write('<h1>');
document.write('身長が'+height+'cmの人の標準体重は');
document.write(weight+'kgです。');
document.write('<h1>');
}
</script>
</head>
<body>
<h1>標準体重を求めるプログラム</h1>
<p><button onclick="bmi()">標準体重を求めます</button></p>
</body>
男性か女性かの入力をして標準体重を求めるプログラム
<style>
button {
font-size: 24px;
cursor: pointer;
}
</style>
<script>
function bmi() {
var height;//身長
var weight;//体重
//男性か女性かを入力
var man = confirm('あなたは男性ですか?\n男性なら「OK」女性なら「キャンセル」を選択してください。');
height = prompt('あなたの身長を入力してください。','170');
console.log(man);
console.log(height);
height = parseInt(height);
console.log(height);
//計算を行う
if (man) {
weight = ( height - 80 ) * 0.7;
} else {
weight = ( height - 70 ) * 0.6;
}
weight = (height - 100) * 0.9;
console.log(weight);
document.write('<h1>');
if (height === null) {
document.write('処理がキャンセルされました!');
} else if (man) {
document.write('身長が'+height+'cmの男性の標準体重は');
document.write(weight+'kgです。');
} else {
document.write('身長が'+height+'cmの女性の標準体重は');
document.write(weight+'kgです。');
}
document.write('<h1>');
}
</script>
</head>
<body>
<h1>男女別の標準体重を求めるプログラム</h1>
<p><button onclick="bmi()">標準体重を求めます</button></p>
</body>
季節を求めるプログラム
<style>
button {
font-size: 26px;
cursor: pointer;
}
</style>
<script>
function season() {
var month = prompt('月を入力してください。','例)1月なら1');
console.log(month);
var imonth = parseInt(month);
console.log(imonth);
document.write('<h1>');
if(month === null) {
document.write('処理がキャンセルされました!');
} else if(imonth >= 3 && imonth <=5 ) {
document.write(imonth+'月は春です。');
} else if(imonth >= 6 && imonth <= 8) {
document.write(imonth+'月は夏です。');
} else if(imonth >= 9 && imonth <= 11) {
document.write(imonth+'月は秋です。');
} else if(imonth >= 1 && imonth <= 2 || imonth === 12) {
document.write(imonth+'月は冬です。');
} else {
document.write('1から12の整数を半角で入力してください。');
}
document.write('</h1>');
}
</script>
</head>
<body>
<h1>季節を求めるプログラム</h1>
<p>1から12の整数を半角で入力してください。</p>
<p><button onclick="season()">季節を求めます</button></p>
</body>