Forum Moderators: open
With "Choices()"... as in
function choice1() { m = Math.random()
if (m < 0.5) return 3; else return 4}
function choice2() { m = Math.random()
if (m > 0.5) return 4; else return 3}
function choice3() { m = Math.random()
if (m < 0.5) return 3; else return 4}
var x = choice1()
var y = choice2()
var z = choice3()
var P = (x + y + z)
function Choices() {
alert(' first choice ' + x + ' \n\n second choice ' + y + ' \n\n third choice ' + z )}
The total of the three choices is either 9,10, 11, or 12.
How to add another line of text to the alert which depends on the total (eg, if the total is 9 the text would be "you've won a bike!" and if 10, "you've won a trip!" etc)?
It looks like a switch function with P, but I can't see how to integrate that with the alert. Does the alert have to be in each case?
I hope this is what you want:
function choice() {
var m = Math.random();
return(m<0.5?3:4)
}function choices() {
var total = 0, tmp, str = '';
for(var i=0; i<3; i++) {
tmp = choice();
str += 'Choice no. ' + (+i+1) +': ' + tmp + '\n';
total += tmp;
}
switch(total) {
case 9:
str += '\n You get a bike';
break;
case 10:
str += '\n You get a trip';
break;
case 11:
str += '\n You get a beer';
break;
case 12:
str += '\n You get a boat';
break;
default:
}
alert(str)
}
'default' as last case works in this:
function firstChoice() { r = Math.random()
if (r < 0.5) return 3; else return 4}
function secondChoice() { r = Math.random()
if (r > 0.49) return 4; else return 3}
function thirdChoice() { r = Math.random()
if (r < 0.51) return 3; else return 4}
//useful to keep the three functions separate so the odds could be loaded occasionally
function Choices() {
var a = firstChoice()
var b = secondChoice()
var c = thirdChoice()
var d = (a + b + c)
switch(d) {
case 9:
alert( 9 + ' green to yellow ');
break
case 10:
alert( 10 + ' green to white ');
break
case 11:
alert( 11 + ' black to blue ');
break
default:
alert( 12 + ' green to red ');}
}
punching above my weight here...
It's a quibble, but if you're still interested, it would be good to retain the three slightly different math.random functions, so the odds can be loaded up occasionally. Klutz version:
function firstChoice() { r = Math.random()
if (r < 0.5) return 3; else return 4}
function secondChoice() { r = Math.random()
if (r > 0.49) return 4; else return 3}
function thirdChoice() { r = Math.random()
if (r < 0.51) return 3; else return 4}
function Choices() {
var a = firstChoice()
var b = secondChoice()
var c = thirdChoice()
var d = (a + b + c)
switch(d) {
case 9:
alert(' first choice ' + a + '\n\n second choice ' + b + '\n\n third choice ' + c + '\n\n grolsch ')
break;
case 10:
alert(' first choice ' + a + '\n\n second choice ' + b + '\n\n third choice ' + c + '\n\n hair of the dog ')
break;
case 11:
alert(' first choice ' + a + '\n\n second choice ' + b + '\n\n third choice ' + c + '\n\n redback ')
break;
default:
alert(' first choice ' + a + '\n\n second choice ' + b + '\n\n third choice ' + c + '\n\n grange ')}
}
Thirsty work: Cheers.