/    Sign up×
Community /Pin to ProfileBookmark

long explanation, but solution will be short !

I’m missing a really simple bit of syntax: how to take the output of a math.random() call, and feed it to another variable:

[CODE]function Coin1() {
var m1 = Math.random()
if (m1 < 0.5) return 2; else return 3 }[/CODE]

Here, a score of 2 counts as “tails” and 3 as “heads”.

When we generate a report, it needs to say “Coin 1: score 3, heads” or “Coin 1: score 2, tails”. So we need to turn 2 and 3 into tails and heads, with something like:

[CODE]function numtoHT() {
if “return_of_m1” = 3, return “heads”; else return “tails” } [/CODE]

What is the syntax for “return_of_m1”?

Cheers

version with a more comments:
[url]http://www-personal.usyd.edu.au/~ctillam/test.htm[/url]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@Wisest_GuyDec 03.2007 — [CODE]function numtoHT(){
if (Coin1() == 3){
return "heads";
}else{
return "tails";
}[/CODE]

Or
[CODE]function numtoHT(){
return (Coin1() == 3) ? "heads" : "tails";
}[/CODE]

They both do the same thing.
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — Hey, many thanks, I like em both. Especially the second.
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — ...and if there are three coins, and it needs to be generic rather than writing it three times
[CODE]function numtoHT(){
return (Coin() == 3) ? "heads" : "tails";
}[/CODE]

how to identify which coin? It's not like they have ids, they're separate functions...
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — don't worry, its only 2 more lines of code! Sorry.

BUT:

neither version is correct.

If you look at the updated test page, you'll see that "3" and "2" do not always match "heads" and "tails".

What's happening is that your code runs math.random() again! we need the already-established output, not the function... that's where I got stuck.

cheers
Copy linkTweet thisAlerts:
@Wisest_GuyDec 03.2007 — When you have a function that returns something:
[CODE]function func(){return 1;}[/CODE]
You can only get that value by storing the result when you call the function:
[CODE]var ret = func();[/CODE]
So if you need to use the result of Coin1 more than once, store it in a variable.
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — ok, thanx
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — When you have a function that returns something:
[CODE]function func(){return 1;}[/CODE]
You can only get that value by storing the result when you call the function:
[CODE]var ret = func();[/CODE]
So if you need to use the result of Coin1 more than once, store it in a variable.[/QUOTE]


Been giving the error console a workout: every variation I've tried seems to run math.random again.

Think you may have to spell it out for me...
Copy linkTweet thisAlerts:
@Wisest_GuyDec 03.2007 — Replace:
[CODE]
function numtoHT1(){
if (Coin1() == 3){
return "heads";
}else{
return "tails";
}}

function numtoHT2() {
return (Coin2() == 3) ? "heads" : "tails"
}

var a = "Coin 1: &nbsp;" + Coin1() + "&nbsp; &#8212; &nbsp;" + numtoHT1() + "<br /><br />"
var b = "Coin 2: &nbsp;" + Coin2() + "&nbsp; &#8212; &nbsp;" + numtoHT2() + "<br /><br />"
var c = "Coin 3: &nbsp;" + Coin3() + "&nbsp; &#8212; &nbsp; bugger!"
[/CODE]

With:
[CODE]
function numtoHT(num) {
return (num == 3) ? "heads" : "tails"
}

var a = Coin1();
var b = Coin2();
var c = Coin3();
a = "Coin 1: &nbsp;"+a+"&nbsp; &#8212; &nbsp;"+numtoHT(a)+"<br /><br />"
b = "Coin 2: &nbsp;"+b+"&nbsp; &#8212; &nbsp;"+numtoHT(b)+"<br /><br />"
c = "Coin 3: &nbsp;"+c+"&nbsp; &#8212; &nbsp; bugger!"
[/CODE]
Copy linkTweet thisAlerts:
@ctozauthorDec 03.2007 — http://www-personal.usyd.edu.au/~ctillam/test2.htm


No hurry, but if you have time sometime: how does it work, having the variables "a", "b" and "c" each with two values? I'm a cutnpaster rather than a writer, as you've probably worked out, and evry explanation helps.

cheers & thanks
Copy linkTweet thisAlerts:
@Wisest_GuyDec 04.2007 — You'll want to use an array for that:
[CODE]
var a = [value1,value2];// An array with 2 values
alert ("Value 1 is +"a[0]+"nValue 2 is "+a[1]);
[/CODE]
Copy linkTweet thisAlerts:
@ctozauthorDec 05.2007 — you're way ahead of me there... I asked the wrong question.

What I'm looking for is an explanation of how, in your solution, "a" seems to stand both for the function "Coin1()" and for an innerHTML string:

[CODE]var a = Coin1()

a = "Coin 1: &nbsp;"+ a +"&nbsp; — &nbsp;"+numtoHT(a)+"<br /><br />"
[/CODE]

cheers
Copy linkTweet thisAlerts:
@Declan1991Dec 05.2007 — you're way ahead of me there... I asked the wrong question.

What I'm looking for is an explanation of how, in your solution, "a" seems to stand both for the function "Coin1()" and for an innerHTML string:

[CODE]var a = Coin1()

a = "Coin 1: &nbsp;"+ a +"&nbsp; — &nbsp;"+numtoHT(a)+"<br /><br />"
[/CODE]

cheers[/QUOTE]

The variable is being redefined. as a new value. Just like:
[code=php]
var a = 10;
a = a*2;
a = "Hello"+a;
[/code]
×

Success!

Help @ctoz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...