3の倍数の判別
JavaScriptの基礎
ex.1
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <meta charset="UTF-8"> <title>3の倍数を判別</title> </head> <body> <h1>3の倍数を判別</h1> <p>   <button onClick="btn()">判別する</button> </p> <h2 id="result">ここに結果を表示</h2> 	<script> 		function btn(){ 			var num = prompt('1以上の半角数字を入力してください'); 			num = parseInt(num); 			var element = document.getElementById('result'); 			 if (num % 3 === 0) { 				element.textContent = '入力された数字「' + num + '」は3の倍数です。'; 				}else 				{ 			element.textContent =  '入力された数字「' + num + '」は3の倍数ではありません'; 				} 		} 	</script> </body> </html> | 
ex.2
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <!doctype html> <html> <head> <meta charset="UTF-8"> <title>3の倍数を判別</title> </head> <body> <h1>3の倍数を判別</h1> <p>   <button onClick="btn()">判別する</button> </p> <h2 id="result">ここに結果を表示</h2> <script> 		function btn(){ 			var num = prompt('1以上の半角数字を入力してください'); 			num = parseInt(num); 			var str = '入力された数字「' + num + '」は'; 			var element = document.getElementById('result'); 			if (num < 1 || isNaN(num)) { 				element.textContent = str+ '判別できません'; 				}else if (num % 3 === 0) { 				element.textContent = str+ '3の倍数です。'; 				}else 				{ 			element.textContent = str+ '3の倍数ではありません'; 				} 		} 	</script> </body> </html> | 
 
  
  
  
  
