Welcome to WebmasterWorld Guest from 54.147.217.76

Forum Moderators: open

Message Too Old, No Replies

if.else problem

   
3:10 am on Sep 25, 2011 (gmt 0)

5+ Year Member



Here's the html:
<p><a id="testlink" onclick="Coin(); Test();">test link</a></p>
<p id="c2" style="display:none;">2</p>
<p id="c3" style="display:none;">3</p>


Coin() displays "2" if the output of a random function is less than 0.5, and "3" if the output is greater than or equal to 0.5.

Test() should turn the link green or red depending on the numerical output of Coin(); code below. Problem is, regardless of the value of the random function, Test() always turns the link green: the 'else if' part never runs.

function Coin(){
var c = Math.random();
return (c < 0.5) ? [2, $('#c2').show('slow')] : [ 3, $('#c3').show('slow')];
}
function Test() {
if('Coin() return < 0.5') {$("#testlink").css('color','green')}
else if('Coin() return >= 0.5') {$("#testlink").css('color','red')}
}


Can anyone help? It's not the cache.

cheers
11:12 am on Sep 25, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



There are a few issues with your code...

- Your Coin() function returns an array eg. [2, $('#c2').show('slow')], but your Test() function is expecting a value between 0 and 1.

- Everytime you call Coin() it is going to produce a different (random) result. So the 3 times you call Coin() in your script above (once in the onclick handler and twice in the Test() function) will all be separate results.

- The syntax in your Test() function is incorrect...

if('Coin() return < 0.5')


'Coin() return < 0.5' is just a string in your code, so will always evaluate to true, it is the same as:
if('Hello World') {....}


I assume this should be something like:
if (Coin() < 0.5) {....}


But this still won't work, since Coin() returns an array AND the result will be different from the call to Coin() in your event handler.

Should the call to Test() be dependent on the initial call to Coin()?

What should Coin() return?

Another tip, if you have code like:
if (something < 0.5) { 
// ....
} else if (something >= 0.5) {
// ....
}


This can be simplified (is the same as) to:

if (something < 0.5) { 
// ....
} else {
// ....
}


Since if something is not less than 0.5 it can't be anything else other than greater than or equal to 0.5, assuming that something is indeed numeric.

...a few things to think about :)
9:12 pm on Sep 25, 2011 (gmt 0)

5+ Year Member



Yup, a few things. Thanks. Back to the drawing board.

This is part of a process of adapting a code-heavy but reliable script, in which Test() reflects the output of Coin() using inner HTML. I'm hoping to avoid the inner HTML, but using an array to to both determine and display the output may not be the way to go.
9:13 pm on Sep 25, 2011 (gmt 0)

5+ Year Member



Yup, a few things. Thanks. Back to the drawing board.

This is part of a process of adapting a code-heavy but reliable script, in which Test() reflects the output of Coin() using inner HTML. I'm hoping to avoid the inner HTML, but using an array to to both determine and display the output may not be the way to go.