PDA

View Full Version : If / else statement


maria24
12-16-2010, 09:21 PM
Hello,

Yet another question from this newbie.

I am trying to get an answer to display in a text field based on if/else statement of a new variable, created by what happens to an existing one. But no matter how I configure it, it doesn't want to work.

Here's what I have so far. What am I doing wrong? Thanks for any help.

sliderOne.addEventListener(Event.CHANGE, sliderTotal);


var newOne:Number;
function sliderTotal(evt:Event): void {
total_txt.text = Number(sliderOne.value * 10 + newOne.value * 3).toString();

if (sliderOne.value > 10000) {
newOne.value = Number(sliderOne.value - 10000);
}
else {
newOne.value = 0;
}
}

rrh
12-16-2010, 09:45 PM
Well, if newOne is a Number, you don't need to reference newOne.value, just newOne.

After that, throw a trace() into sliderTotal to double-check whether the function is getting called.

maria24
12-16-2010, 11:14 PM
Thanks for your help, rrh! I will give that a shot.
Thanks again.

the binary
12-17-2010, 12:20 PM
its always a good idea to assign initial values to your variables..


var newOne:Number = 0;

maria24
12-17-2010, 09:00 PM
Hello,

Thanks binary and rrh for your help.

I'm sorry if this is really obvious, but I've set up these sliders so the total shows up in a dynamic text field. It seems to work fine when the sliders are moved forward. But when I move the sliders backward and go back to 0, NaN displays in the textfield. And it seems to happen only to the last slider that's moved back. So if there are three sliders moving back, they're all fine except for the last one.

Any thoughts on what could be causing this?

Thanks again for all your help.

the binary
12-17-2010, 11:04 PM
this is mostly caused by a variable without a value (as i said above)
like:


var foo:Number;
trace( Number(foo * 2.23) ); // NaN

if you want to calculate with variables make sure they have an initial value.

cheers

maria24
12-20-2010, 02:10 PM
Hi binary,

I've assigned initial values as you mentioned.

ie. var newOne:Number = 0;

but still running into that NaN message.

Thanks for your help.

the binary
12-20-2010, 02:20 PM
hm.. its hard to say without code..
could post the complete related and formatetd! code please .. ?

maria24
12-20-2010, 04:01 PM
Hi binary,
Below is the code affected. I think the very last line's causing an issue:
function onetwoTotal (evt:Event): void {
onetwototal_txt.text = "$" + numberFormat(Number(oneTotal + twoTotal) / (sliderOne.value + sliderTwo.value)).toString();

}

As mentioned, the slider works fine when moved forward but when it's moved back to 0, NaN displays. I think the part that's underlined above is what's causing it. Because when I comment it out, NaN disappears.

Thanks for any help on this.

sliderOne.value = 0;
sliderTwo.value = 0;
var oneTotal: Number = 0;
var twoTotal: Number = 0;
var newOne:Number = 0;
var newTwo:Number = 0;

sliderOne.addEventListener(Event.CHANGE, sliderOnetotal);
sliderTwo.addEventListener(Event.CHANGE, sliderTwototal);

sliderOne.addEventListener(Event.CHANGE, onetwoTotal);
sliderTwo.addEventListener(Event.CHANGE, onetwoTotal);


function sliderOnetotal (evt:Event): void {
oneTotal = Number(sliderOne.value * 10.1 + newOne * 3.1);

if (sliderOne.value > 10000) {
newOne = Number(sliderOne.value - 10000);
}
else {
newOne= 0;
}
}


function sliderTwototal (evt:Event): void {
twoTotal= Number(sliderTwo.value * 10.0 + newTwo * 3.0);

if (sliderTwo.value > 2000) {
newTwo = Number(sliderTwo.value - 2000);
}
else {
newTwo = 0;
}
}

function onetwoTotal (evt:Event): void {
onetwototal_txt.text = "$" + numberFormat(Number(oneTotal + twoTotal) / (sliderOne.value + sliderTwo.value)).toString();

}

rrh
12-20-2010, 04:18 PM
Well, if sliderOne.value and sliderTwo.value are set to zero, I think it'll give a NaN when you try to divide by zero. Maybe have an if statement to check for that condition and handle it as a special case?

maria24
12-20-2010, 04:22 PM
Thanks rrh. I'll give that a shot and see what happens.
Thanks for your help.

maria24
12-20-2010, 06:01 PM
I tried something like this but getting all kinds of errors. I'm really messing this up.

function onetwoTotal (evt:Event): void {
onetwototal_txt.text = "$" + numberFormat(Number(oneTotal + twoTotal) / (sliderOne.value + sliderTwo.value)).toString();

if ((oneTotal + twoTotal) = 0) && ((sliderOne.value + sliderTwo.value)=0) {
onetwototal_txt.text = "$0";
else onetwototal_txt.text = "$" + numberFormat(Number(oneTotal + twoTotal) / (sliderOne.value + sliderTwo.value)).toString();
}
}

the binary
12-20-2010, 06:04 PM
well that was not formated.. ;) use the 'code' button next time please..

the easiest way is to split up the calculation and check its value..

trace("slider: "+ sliderOne.value); // correct ?
trace("newOne: "+ newOne); // correct or "NaN" ?

var s1: Number = sliderOne.value * 10.1;
var s2: Number = newOne * 3.1;

trace("--s1: "+ s1);
trace("--s2: "+ s2);

var result: Number = s1 + s2;

trace('--result: '+ result); // NaN ?

oneTotal = result;

maria24
12-20-2010, 06:09 PM
I'm sorry. I'll give that a shot.
Thanks so much.

rrh
12-20-2010, 06:46 PM
You'd get an error from those 'if ()' when you use = instead of ==

One is assignment, one is comparison.

Also, you'd get an error from this:

if (__) {
...
else
...
}It expects it in the form

if (___)
{ ... }
else
{ ... }

maria24
12-20-2010, 06:51 PM
I think rrh you're right about what's causing that NaN. If sliderOne.value plus sliderTwo.value is 0 AND oneTotal and twoTotal = 0, I end up with 0 divided by 0. And that's causing the NaN.

So I'm trying to do the if/else statements that rrh suggested but coming up with compiler errors.

the binary
12-20-2010, 08:08 PM
then simply put an check before you assign the value like

if (isNan(result))
//fall back with an default value..

maria24
12-21-2010, 01:32 PM
Thanks the binary and rrh for your help.

It was being divided by 0 that was causing the issue.

So I added this to the function
if ((sliderOne.value + sliderTwo.value) == 0) {
onetwototal_txt.text = "$0";

and that seemed to eliminate the NaN issue.

Thanks again.