Forum Moderators: open

Message Too Old, No Replies

alert + switch?

feeding different texts to an alert

         

ctoz

10:16 am on Aug 22, 2007 (gmt 0)

10+ Year Member



(thanks to nshiell and daveVK, so far)

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?

Arno_Adams

11:56 am on Aug 22, 2007 (gmt 0)

10+ Year Member



Hi ctoz,

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)
}

ctoz

6:12 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



Arno_ hi,
It looks very like what i want... but: checked the syntax, tried making the final case the default, which has worked for me before, no reserved words there. 3am here, catch u later.

ct :(

Fotiman

6:52 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



case 12:
default:
str += '\n You get a boat';
break;
}

ctoz

2:30 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Not there yet. Not sure the switch function is the problem: 'default' as the last case should work, but neither that, or Fotiman's or Arno's verson does the trick.

'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...

Arno_Adams

6:50 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Hi ctoz,

Please explain why my example doesn't meet your requirements?

ctoz

8:03 am on Aug 23, 2007 (gmt 0)

10+ Year Member



I couldn't get it to work. It was very late and I had a capitalised C in the link. Duizend verontschuldigingen!

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.

Arno_Adams

11:13 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Good to hear you got it working.

AA