homepage Welcome to WebmasterWorld Guest from 54.161.192.61
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

    
return query
ctoz

5+ Year Member



 
Msg#: 4351295 posted 2:01 am on Aug 14, 2011 (gmt 0)

I have a randomizing function with two outcomes: currently, it returns one of two numbers, which are used as bases for other functions:

function Ran() {
var c = Math.random()
if (c < 0.5) return 2; else return 3;
}

For each outcome, what would be the syntax for returning a function as well as a number, eg:

function doA() {...}
function doB() {...}

// should these be oustide or inside Ran() ?

function Ran() {
var c = Math.random()
if (c < 0.5) return 2; doA(); else return 3; doB();
// use semi-colon after returns ?
}

 

astupidname

5+ Year Member



 
Msg#: 4351295 posted 9:25 am on Aug 14, 2011 (gmt 0)

returning a function as well as a number

Do you actually mean the results of another function? Or an actual other function? Since you show the functions being invoked, I will assume the results are what you want. Return an array where the first item in the array is the number and second item is the result of the function:

function Ran() { 
var c = Math.random();
return (c < 0.5) ? [2, doA()] : [3, doB()];
}

ctoz

5+ Year Member



 
Msg#: 4351295 posted 10:55 am on Aug 14, 2011 (gmt 0)

OK, thanks: that's what I was looking for

ctoz

5+ Year Member



 
Msg#: 4351295 posted 3:29 am on Aug 15, 2011 (gmt 0)

Ok: now I have three random functions which return a number and a function (the function displays the number, after a randomised delay):

function coinM(){
var c = Math.random();
return (c < 0.5) ? [2, $('#Cm2').delay(dl()).animate({opacity:1.0}, 100)] : [ 3, $('#Cm3').delay(dl()).animate({opacity:1.0}, 100)];
}
function coinR(){
var c = Math.random();
return (c < 0.5) ? [2, $('#Cr2').delay(dl()).animate({opacity:1.0}, 100)] : [ 3, $('#Cr3').delay(dl()).animate({opacity:1.0}, 100)];
}
function coinL(){
var c = Math.random();
return (c < 0.5) ? [2, $('#Cl2').delay(dl()).animate({opacity:1.0}, 100)] : [ 3, $('#Cl3').delay(dl()).animate({opacity:1.0}, 100)];
}

wrapped together with

function showcoins() {
clearcoins(); // hide any span with charAt(0)=C
setTimeout(function(){ coinM();coinR();coinL(); },1600);
}


The next step is to extract the numerical output of each 'coin' function, sum them, and depending on the sum, do different things: (switch function with cases 6, 7, 8, and default 9).

So… how do you extract the numerical output of the 'coin' functions?
var M = coinM() etc, doesn't do it.
I can see its the first term out of the array... I'll be experimenting, but hoping for mor ehelp.

cheers.

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