Skip to content

Instantly share code, notes, and snippets.

@nekonenene
Created April 7, 2016 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekonenene/a1794022bb5a3ce3e930b5dd2b9c42ff to your computer and use it in GitHub Desktop.
Save nekonenene/a1794022bb5a3ce3e930b5dd2b9c42ff to your computer and use it in GitHub Desktop.
ES6 を用いたユークリッド互除法。しかし displayGcd メソッド内のログ出力は undefined となる
$(function()
{
var gcd = new GreatestCommonDivisor(14, 21);
var outputElement = $(".output");
outputElement.append( gcd.displayGcd() );
});
class GreatestCommonDivisor
{
constructor(_a = 1, _b = 1)
{
this.a = _a;
this.b = _b;
}
/* ユークリッド互除法で最大公約数を求め、返す */
static euclid(_a, _b)
{
var a = _a; var b = _b;
var remainder = a % b;
// console.log(remainder + " : " + b);
if(parseInt(remainder) === 0)
{
return parseInt(b);
}
this.euclid(b, remainder);
}
/* 最大公約数を案内する文字列を返す */
displayGcd()
{
var gcd = GreatestCommonDivisor.euclid(this.a, this.b);
console.log( gcd );
var string = this.a + " と " + this.b + " の最大公約数は " + gcd;
return string;
}
}
@ConquestArrow
Copy link

こんにちは。returnが抜けているようです。

    /* ユークリッド互除法で最大公約数を求め、返す */
    static euclid(_a, _b)
    {
        var a = _a; var b = _b;
        var remainder = a % b;

        // console.log(remainder + " : " + b);
        if(parseInt(remainder) === 0)
        {
            return parseInt(b);
        }
        return this.euclid(b, remainder);
    }

Babel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment