-
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
-
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.
-
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]
-
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
-
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.
-
Yes, i was actually reading that part
Your help has been precious! thanks a lot jweaver.
gparis
-
Why dont you guys use modulo ?
result=numberVar%2;
1 is odd, 0 is even.
-
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.
-
Cut it even smaller.
on (release) {
writeAnswer("This Number is "+((numEnter%2)?"Odd .":"Even ."));
}
-
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 ."));
}
-
oups... i looked in the reference guide for the "modulo" and the explanation is not the clearest... what calculation does "%2" do???
thanks!
gparis
-
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?
-
Senior Member
4.4%2 = 0.4, but isn't it even?
-
hi leo lima!
you know how to break a party, don't you? (just kidding)
gparis
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|