homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
if.else problem
ctoz

5+ Year Member



 
Msg#: 4366860 posted 3:10 am on Sep 25, 2011 (gmt 0)

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

 

penders

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



 
Msg#: 4366860 posted 11:12 am on Sep 25, 2011 (gmt 0)

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 :)

ctoz

5+ Year Member



 
Msg#: 4366860 posted 9:12 pm on Sep 25, 2011 (gmt 0)

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.

ctoz

5+ Year Member



 
Msg#: 4366860 posted 9:13 pm on Sep 25, 2011 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved