views:

74

answers:

2

Hello.

I have what I think should be an easy problem. I read through an array and count the occurrences of the values in it, I then write these to some variables, which I want to compare in a conditional. My conditional should trace which number occurs more.

Array code:

ActionScript Code:

private function runCount(scoreArray:Array, count1:Number, count2:Number, count3:Number):void {
    for (var i:int=0; i < scoreArray.length; i++) {
        if (scoreArray[i] == count1) {
        _1count++;// = _1count + 1;
        trace("1Count is:",_1count);
        }
    }

    for (var o:int=0; o < scoreArray.length; o++) {
        if (scoreArray[o] == count2) {
        _2count++;// = _2count + 1;
        trace("2Count is:",_2count);
        }
    }

    for (var p:int=0; p < scoreArray.length; p++) {
        if (scoreArray[p] == count3) {
        _3count++;// = _3count + 1;
        trace("3Count is:",_3count);
        }
    }
    runFinal();
}

And conditional code:

ActionScript Code:

public function runFinal():void {
    if (_1count > _2count || _3count) {
        trace("more one than anything else");
    } else if (_2count > _1count || _3count) {
        trace("more two than anything else");
    } else if (_3count > _1count || _2count) {
        trace("more three than anything else");
    }
}

Now, I get no errors, but my conditional gets very confused and spits out random results. Any ideas?

Thank you.

Update: As an example, this is what it gives me.

1Count is: 1
1Count is: 2
2Count is: 1
2Count is: 2
3Count is: 1
3Count is: 2
3Count is: 3

more one than anything else

+1  A: 

When you write:

public function runFinal():void {
    if (_1count > _2count || _3count) {
        trace("more one than anything else");
    } else if (_2count > _1count || _3count) {
        trace("more two than anything else");
    } else if (_3count > _1count || _2count) {
        trace("more three than anything else");
    }
}

your conditionals look misguided. As written, if the final term of the conditional ever evaluates to true (non-zero, etc.), the conditional statement will evaluate to true, which I don't think is what you intend.

Are you trying to say "if _1count is greater than either _2count or _3count"? If so, you would write that as

if ( (_1count > _2count) || (_1count > _3count) ) 

In any case, it is really not clear what you are trying to do.

Robusto
Hello. Thanks for that. What I'm doing is on a button click, I push a number into an array, (1, 2, or 3). When you click a fourth button, I iterate through the array and check for occurrences of those numbers, add them up, and write them to a set of variables. I then check which number is greatest, so I know which button was pushed the most amount of times. Now I guess I could just have a variable increased by +1 on every button click, but I'm learning about different ways to do things. :) I can post the entire code if that helps?
Peter
If you want a useful answer, posting all the relevant code always helps.
Robusto
+2  A: 

Robusto is correct, you have an error in your if statement. What the first if is actually checking for is if _1count is greater than _2count or _3count is greater than 0. So _1count could be 10, _2count could be 15, and _3count could be 5 and this would evaluate to true.

You could use an array of object and sort them to get the correct answer, this would save writing more and more conditions if you increase the number of items you want to count. It would go something like this:

var count0:Counter = new Counter('count0');
var count1:Counter = new Counter('count1');
var count2:Counter = new Counter('count2');
var counters:Vector.<Counter> = Vector.<Counter>([count0, count1, count2]);

counters[0].value = 101;
counters[1].value = 67;
counters[2].value = 172;

counters.sort(sortOnValue, Array.DESCENDING);

trace('The highest value was: ' + counters[0].name);  // The highest value was: count2

function sortOnValue(a:Object, b:Object):Number{
    if(a.value > b.value) {
        return 1;
    }else if(a.value < b.value) {
        return -1;
    }else{
        //a.value == b.value
        return 0;
    }
}

class Counter {
  public var name:String;
  public var value:int;

  public function Counter(name:String){
    this.name = name;
  }
}

See the documentation for Vector.: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html?filter_flash=cs5&amp;filter_flashplayer=10.1&amp;filter_air=2#sort()

OK, method number 2:

var count0:Object = {name:'count0', value:0};
var count1:Object = {name:'count1', value:0};
var count2:Object = {name:'count2', value:0};
// Vector doesn't have the sortOn method, so use an Array instead
var counters:Array = [count0, count1, count2];

counters[0].value = 101;
counters[1].value = 67;
counters[2].value = 172;

counters.sortOn('value', Array.DESCENDING | Array.NUMERIC);

trace('The highest value was: ' + counters[0].name);

See the documentation for Array for more ways of doing the same: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html#sort()

Or, a mixture of both:

var count0:Counter = new Counter('count0');
var count1:Counter = new Counter('count1');
var count2:Counter = new Counter('count2');
var counters:Array = [count0, count1, count2];

counters[0].value = 101;
counters[1].value = 670;
counters[2].value = 1720;

counters.sortOn('value', Array.DESCENDING | Array.NUMERIC);

trace('The highest value was: ' + counters[0].name);

class Counter {
  public var name:String;
  public var value:int;

  public function Counter(name:String){
    this.name = name;
  }
}
Joony
+1... even better would be to use Vector instead of Array since its contents will all have the same Object datatype.
TheDarkInI1978
Even even better would be to create a specific object (a DTO) with name and value as properties. That way the player wouldn't have to bother with a hash table to keep track of the dynamic objects' properties (= faster in less memory).
Joony
+1. Although you could get rid of the compare function if you just use the `sortOn` method. Like this: `counters.sortOn("value", Array.DESCENDING | Array.NUMERIC);`
Juan Pablo Califano
True, but it should be noted that you can't use a Vector instead of Array then, as Vector doesn't have the sortOn method.
Joony
I don't really understand why as sortOn would better suit Vector.
Joony