入力した月の季節を表示するスクリプト
授業でのスクリプトを自分なりに書いてみました。
HTML
<h1>月別に季節を判別</h1> <p>ボタンをクリックすると月を入力するウィンドウが表示されます。</p> <p><button id="btn">月別に季節を判別</button></p>
window.onload = function(){ distinctionMonth(); } function distinctionMonth(){ var btn = document.getElementById('btn'); btn.onclick = function(){ var month = ''; month = prompt('月を入力してください。1-12を半角数字で入力'); if((month == 1)||(month == 2)||(month == 12) ){ console.log('winter'); }else if((month == 3)||(month == 4)||(month == 5)){ console.log('spring'); }else if((month == 6)||(month == 7)||(month == 8)){ console.log('summer'); }else if((month == 9)||(month == 10)||(month == 11)){ console.log('autumn'); }else{ console.log('正しく入力してください'); } }; };