dcsimg
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Odd and Even Numbers detection

  1. #1
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    hi all! I need to tell a variable if a number is odd or even. I know that the main difference between odd and even is that any "odd" number divided by 2 will be "anyNumber.5" when "even" numbers divided by 2 will be "anyNumber.0" i don't know how to script that "anynumber" part. This is the way the script could look:
    Code:
    on (press) {
    	count = new Number(count+1);
    	if (count/2 == ANYNUMBER.0) {
    		_root.num = "even";
    	} else {
    		_root.num = "odd"; }}
    Of course ANYNUMBER doesn't exist. What could i say instead?
    thanks in advance
    gparis

  2. #2
    Senior Member
    Join Date
    May 2001
    Posts
    180
    I have an answer, here is the script:

    on (release) {
    divide = numEnter / 2;
    test = new String (divide);
    check = test.indexOf(".5");
    if (check == -1) {
    writeAnswer = "This Number is Even";
    } else {
    writeAnswer = "This Number is Odd";
    }
    }

    numEnter = input text variable to enter number
    writeAnswer = dynamic text variable to print odd/even answer

    This works like you had yours. It takes the number, divides it by 2, and than checks for the '.5' through the whole string. If it doesn't find it, than it returns -1 and tells writeAnswer to print 'This Number is Even', but if it does find '.5' in the string, than the index number of the point where it was found is returned, but that is of no use to us, so it just goes to the 'else' part of the if () statement, and sets writeAnswer to 'This Number is Odd'.

    Hope that makes sense and helps you out.

  3. #3
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141

    thanks!

    hi jweaver,
    Amazing! thanks a lot
    one question: why does it return "-1" if the ".5" is not found in the string?
    gparis
    [Edited by gparis on 06-13-2001 at 02:52 AM]

  4. #4
    Senior Member
    Join Date
    May 2001
    Posts
    180
    This is Flash's default return for the .indexOf() method if the specified value to be found, is not found.

    You can find out how this works in your actionscript help files: Help>Actionscript Dictionary> then look under the 'S' for String.indexOf

  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    180
    Reading through the help files, i've found that the '-1' in the world of strings means the last character of a string.

    So, i guess when the .indexOf() method is envoked, and it returns '-1' its just saying that it has checked to the last character of your string and the value you specified could not be found.

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Yes, i was actually reading that part
    Your help has been precious! thanks a lot jweaver.
    gparis

  7. #7
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    Why dont you guys use modulo ?

    result=numberVar%2;

    1 is odd, 0 is even.

  8. #8
    Senior Member
    Join Date
    May 2001
    Posts
    180
    The modulo (%) operator would simplify it, so...

    on (release) {
    result = numEnter % 2;
    if (result == 0) {
    writeAnswer = "This Number is Even";
    } else {
    writeAnswer = "This Number is Odd";
    }
    }

    Cuts it by two lines, good hunting dude.

  9. #9
    Senior Member
    Join Date
    May 2001
    Posts
    1,838


    Cut it even smaller.

    on (release) {
    writeAnswer("This Number is "+((numEnter%2)?"Odd .":"Even ."));
    }



  10. #10
    Senior Member
    Join Date
    Nov 2000
    Posts
    237
    if
    on (release) {
    writeAnswer("This Number is "+((numEnter%2)?"Odd .":"Even ."));
    }

    not working just add "=" after writeAnswer

    like
    on (release) {
    writeAnswer=("This Number is "+((numEnter%2)?"Odd .":"Even ."));
    }

  11. #11
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    oups... i looked in the reference guide for the "modulo" and the explanation is not the clearest... what calculation does "%2" do???
    thanks!
    gparis

  12. #12
    Senior Member
    Join Date
    May 2001
    Posts
    180
    it divides the first number by the second number, and returns the remainder.

    For example:

    4 % 2 returns 0
    5 % 2 returns 1
    9 % 5 returns 4
    4.5 % 2 returns .5

    Get the idea?

  13. #13
    Senior Member Leo Lima's Avatar
    Join Date
    Jul 2000
    Location
    São Paulo, Brazil
    Posts
    745
    4.4%2 = 0.4, but isn't it even?

  14. #14
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    hi leo lima!
    you know how to break a party, don't you? (just kidding)
    gparis

  15. #15
    Senior Member Leo Lima's Avatar
    Join Date
    Jul 2000
    Location
    São Paulo, Brazil
    Posts
    745
    And how to fix it to
    Try this function:
    Code:
    function isOdd(number) {
      if (number < 0) number *= -1; //handles negatives
      while ((number % 2 != 0) && (number % 2 != 1)) // handles not integers
        number *= 10;
      if (number % 2)
        return true;
      else
        return false;
    }
    Tested with:
    Code:
    trace(isOdd(-4));
    trace(isOdd(-3));
    trace(isOdd(4));
    trace(isOdd(3));
    trace(isOdd(-4.4));
    trace(isOdd(-3.3));
    trace(isOdd(-4.3));
    trace(isOdd(-4.41));
    trace(isOdd(-3.32));
    trace(isOdd(-4.33));
    trace(isOdd(4.4));
    trace(isOdd(3.3));
    trace(isOdd(4.3));
    trace(isOdd(4.41));
    trace(isOdd(3.32));
    trace(isOdd(4.33));
    which returned:
    Code:
    false
    true
    false
    true
    false
    true
    true
    true
    false
    true
    false
    true
    true
    true
    false
    true
    Is it good? Now you now if a number isOdd or not
    Now, to apply to your problem:
    Code:
    on (release) { 
      if (!isOdd(numEnter)) { 
        writeAnswer = "This Number is Even"; 
      } else { 
        writeAnswer = "This Number is Odd"; 
      } 
    }
    Regards,
    Leo Lima

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center