user10086072
user10086072

Reputation:

Error in ActionScript

I am coding a fraction simplifier with ActionScript 3 and the following code is finding the gcd for me. However, I am getting an error which states Argument count mismatch. ArgumentError: Error #1063: Argument count mismatch. Expected 2, got 1. Please Advise. Thanks!

 btnDetermine.addEventListener(MouseEvent.CLICK, gcd);

        var no1:Number;
        var no2:Number;

        no1 = Number(txtinno1.text);
        no2 = Number(txtinno2.text);

        function gcd(no1, no2){
            if (no1 == 0 || no2 == 0){
                return 0; 
            }

            if (no1 == no2){
                return no1;
            }



            if (no1 > no2){
                return gcd(no1 - no2, no2);
            }

            else {
                return gcd(no1, no2-no1);
        }
    }

Upvotes: 0

Views: 45

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

You are defining the gcd method with two required parameters (no1 and no2):

function gcd(no1, no2)

However, you are also using that method as a mouse click handler in this line:

 btnDetermine.addEventListener(MouseEvent.CLICK, gcd);

So when that click event fires, it calls the gcd method and passes the MouseEvent as the first argument (and nothing else). Since the function/method is expecting two arguments, you get the error.

I'd guess what you really want to do, is the following:

//add the click listener to a new function
btnDetermine.addEventListener(MouseEvent.CLICK, clickHandler);

//have the click handler take a single MouseEvent as the only argument
function clickHandler(e:MouseEvent):void {
    //inside this click method, get your numbers.
    var no1:Number = Number(txtinno1.text);
    var no2:Number = Number(txtinno2.text);

    //call the gcd function
    gcd(no1, no2);
}

function gcd(no1:Number, no2:Number):Number {
    if (no1 == 0 || no2 == 0){
        return 0; 
    }

    if (no1 == no2){
        return no1;
    }

    if (no1 > no2){
        return gcd(no1 - no2, no2);
    }
    else {
        return gcd(no1, no2-no1);
    }
}

Upvotes: 3

Related Questions