if文までの授業内サンプル

消費税計算のプログラム

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>消費税計算のプログラム</title>
<script>
function taxPrice() {
//閲覧者の入力の値を取得
var price = prompt( '価格を半角数字で入力して下さい', '' );
//prompt()で習得した値は、文字列になります
//文字列は、数値に変換しないと演算には使用できません
//文字列を、数値に変換するために parseInt()メソッドを使用します
prece = parseInt( price );
    //コンソールに取得した値を出力する(表示する)
    console.log( price );
var num = prompt( '個数を半角数字で入力して下さい', '' );
num = parseInt( num );
    console.log( num );

//消費税率 taxRate
var taxRate = 0.08;
//消費税金額 tax
var tax = price*num*taxRate;
    console.log( tax );
//消費税込みの金額 total
var total = price*num + tax;

//出力
var str = '1個の価格が' + price + '円の商品を、' + num + '個買った時の消費税込みの金額は、'+ total + '円です。';
document.write( str );
}
</script>
</head>
<body>
<button onclick="taxPrice()">計算する</button>
</body>
</html>

和暦・西暦の変換プログラム

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>和暦・西暦の変換プログラム</title>
<script>
function fullYear() {
var heisei;
var fullYear;
var str;

heisei = prompt( '平成の数字を半角で入力してください', '' );
fullYear = parseInt( heisei ) + 1988;
str = '平成' + heisei + '年を西暦に変換すると、西暦' + fullYear + '年になります。';

document.write( str );
}
</script>
</head>
<body>
<p><button onclick="fullYear()">変換する</button></p>
</body>
</html>

偶数・奇数の判別プログラム

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>偶数・奇数の判別プログラム</title>
<script>
function everOdd() {
  var num = prompt( '1から10までの半角数字を入力して下さい。', '' );
  num = parseInt( num );
  if( num === 0 ) {
    var even = document.getElementById( 'ans' );
    even.textContent = '入力された数字は' + num + 'です。1から10までの半角数字を入力して下さい。';
  }
  else if( num%2 === 0 ) {
    var even = document.getElementById( 'ans' );
    even.textContent = num + 'は、偶数です。';
  }
  else if( num%2 === 1 ) {
    var even = document.getElementById( 'ans' );
    even.textContent = num + 'は、奇数です。';
  }
  else {
    var even = document.getElementById( 'ans' );
    even.textContent = '入力された値は、判別できません。1から10までの半角数字を入力して下さい。';
  }
}
</script>
</head>
<body>
<h1>偶数・奇数の判別プログラムをDOMで表示する</h1>
<p><button onclick="everOdd()">判別する</button></p>
<p id="ans"></p>
</body>
</html>

月数から四季を判別するプログラム

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>月数から四季を判別するプログラム</title>
<script>
function ansSeason() {
var month = prompt( '月(1~12)の数字を半角数字で入力して下さい', '' );
month = parseInt( month );
if( month === 12 || month === 1 || month === 2 ) {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent = month + '月は、冬です。';
}
else if( (month >= 3) && (month <= 5) ) {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent = month + '月は、春です。';
}
else if( (month >= 6) && (month <= 8) ) {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent = month + '月は、夏です。';
}
else if( (month >= 9) && (month <= 11) ) {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent = month + '月は、秋です。';
}
else if( isNaN(month) ) {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent =  '半角数字ではありません。もう一度 1~12までの半角数字を入力して下さい。';
}
else {
    var seasonName = document.getElementById( 'season' );
    seasonName.textContent = month + 'は、1~12までの半角数字ではありません。もう一度 1~12までの半角数字を入力して下さい。';
}
}
</script>
</head>
<body>
<h1>月数字から四季を判別するプログラム</h1>
<p><button onclick="ansSeason()">判別する</button></p>
<p id="season"></p>
</body>
</html>