<script src="gcmlcm_class.js" type="text/javascript"></script> </head> <body> <h1>Class File</h1> 【実行画面】 <script type="text/javascript"> var cls = new gcmlcm(32, 24); ans = cls.gcm(); document.write("GCM(32,24): ", ans, " <br>"); ans = cls.lcm(); document.write("LCM(32,24): ", ans, " <br>"); </script> |
<script src="gcmlcm_class.js" type="text/javascript"></script> </head> |
<script type="text/javascript"> var cls = new gcmlcm(32, 24); ans = cls.gcm(); document.write("GCM(32,24): ", ans, " <br>"); ans = cls.lcm(); document.write("LCM(32,24): ", ans, " <br>"); </script> |
function gcmlcm(n, m) { this.n = n; //値1 this.m = m; //値2 this.nm = n * m; this.gcm = function() { while(this.n!=this.m) { if (this.n>this.m) this.n -= this.m; else this.m -= this.n; } return this.n; } this.lcm = function() { w = this.gcm(); return this.nm / w; } } |