PDA

View Full Version : return 0 if another variable is 0



ChrisVersion2
12-08-2008, 08:51 PM
Howdy Folks,

I'm trying to have var x equal zero if var y equals 0.

if var y is greater than 0 than war x will equal 240

This is the mess that I've coded. Novice level one I'm fully aware, any help or a direction would be great.



<script type="text/javascript">
var y = 15;
var x = wagemin;

wagemin()
{

if (wth <= 0)
{
return 0;
}
else
{
return 240;}
}
document.write(x)
</script>

Twey
12-09-2008, 12:59 AM
var y = 15,
x = y && 240;

Strangeplant
12-09-2008, 01:09 PM
Ummm, I think you meant to say
var y = 15,
x = wth && 240;

Twey
12-09-2008, 04:15 PM
Well, I was going by the description rather than the provided code. I'm guessing the mysterious wth variable was an error. :p

jscheuer1
12-09-2008, 04:21 PM
wth is undefined. Twey's code is right, except for negative values of y. This would fit the desired outcome stated by the OP about as well as can be divined:


var y = 15,
x = Math.max(0, y) && 240;


I'm trying to have var x equal zero if var y equals 0.

if var y is greater than 0 than war x will equal 240

It is left to the imagination what should happen if y is less than zero, but the pseudo code seems to indicate that x should be zero in that case. But negative values of y may be impossible, or the desired outcome for x in such a case may still be 240. If either of those cases are true, Twey's elegantly simple code is all that's required.

However, it's possible that yet another outcome is wanted for negative y.

magicyte
12-09-2008, 10:21 PM
primitive version (hehe- just wanted to show a different way):


var y = 0, x;
if(y <= 0) x = 0;
else if(y > 0) x = 240;

-magicyte

jscheuer1
12-10-2008, 12:48 AM
I'm trying to have var x equal zero if var y equals 0.

if var y is greater than 0 than war x will equal 240

This is the mess that I've coded. Novice level one I'm fully aware, any help or a direction would be great.

Hopefully all of us have already provided enough code examples to get you on your way, but if not - please answer:

What if y is less than zero, what then? And what's war x?

If you already have your answer, let us know that too, so we can close this thread.