Welcome to WebmasterWorld Guest from 54.162.164.247

Forum Moderators: open

Message Too Old, No Replies

# return query

#### ctoz

2:01 am on Aug 14, 2011 (gmt 0)

#### Junior Member

joined:July 4, 2007
posts:115

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

9:25 am on Aug 14, 2011 (gmt 0)

#### Preferred Member

joined:Aug 18, 2008
posts:408

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

10:55 am on Aug 14, 2011 (gmt 0)

#### Junior Member

joined:July 4, 2007
posts:115

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

#### ctoz

3:29 am on Aug 15, 2011 (gmt 0)

#### Junior Member

joined:July 4, 2007
posts:115

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.