Welcome to WebmasterWorld Guest from 50.16.68.229

Forum Moderators: open

return query

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

5+ Year Member



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 ?
}
9:25 am on Aug 14, 2011 (gmt 0)

5+ Year Member



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()];
}
10:55 am on Aug 14, 2011 (gmt 0)

5+ Year Member



OK, thanks: that's what I was looking for
3:29 am on Aug 15, 2011 (gmt 0)

5+ Year Member



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.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month