PDA

View Full Version : Random number combined with switch does not work



rctxtreme
04-16-2007, 07:22 AM
document.write("<img src='http://www.a2h.8m6.net/images/logo.php?msg=");
// Generates a random number between 1 and 10
var randomnumber=Math.floor(Math.random()*6)
swExmpl(randomnumber);
function swExmpl(ex)
{
switch (ex)
{
case 1: { document.write("now with 10%25 more potatoes!"; break); break }
case 2: { document.write('anger2headshot is not a crack dealer.'); break }
case 3: { document.write("here is your free complementary wastage of your internet bandwith..."); break }
case 4: { document.write("CHUCK NORRIS IS BEHIND YOU!!!"); break }
case 5: { document.write("free lying parrot not provided."); break }
}
}
document.write("'><br>");

This code does not work, can anyone tell me why?

pcbrainbuster
04-16-2007, 07:27 AM
Well I think should tell you this but you probably already know, here goes -

Use if and else if and else instead of switch, because -

1) Its less complicated
2) For all I know switch can not take two or more conditions at once
3) Its more practical...

Inform me of your decision...

rctxtreme
04-16-2007, 07:50 AM
Ah, thanks.

mburt
04-16-2007, 07:50 PM
It's also much more efficient in using arrays then if statements as well (though your point is taken, switch is possibly the worst):


var msg = ["message","message","message","message","message","message"];
for (var i=0;i<msg.length) document.write(msg[Math.floor(Math.random()*6)]);

jscheuer1
04-16-2007, 09:41 PM
You also are better off concatenating the HTML and then writing it out all at once, ex;


var HTMLstr='';
HTMLstr+='<img src="whatever.php?msg=';
HTMLstr+='Some random message';
HTMLstr+='"><br>';
document.write(HTMLstr);

Otherwise, some browsers may automatically close the tag before you intend it to be closed.

Twey
04-16-2007, 10:03 PM
All true, except what pcbrainbuster said, but the reason your original code didn't work are these:
case 1: { document.write("now with 10&#37;25 more potatoes!"; break); break [color=red]}Switch statements use a series of labels, not code blocks (one more reason they should be outlawed :)), and that break has no business being inside a function's brackets. Also, you've failed to account for case 0, and non-Javascript browsers.
<img src="http://www.a2h.8m6.net/images/logo.php?msg=some%20default%20message" alt="Banner" id="banner">
<script type="text/javascript" src="http://www.twey.co.uk/pythonic.js"></script>
<script type="text/javascript">
Twey.Pythonic.LOAD("Array.randomChoice");
document.images['banner'].src = "http://www.a2h.8m6.net/images/logo.php?msg=" +
encodeURIComponent([
"now with 10% more potatoes!",
"anger2headshot is not a crack dealer.",
"here is your free complementary wastage of your internet bandwidth...",
"CHUCK NORRIS IS BEHIND YOU!!!",
"free lying parrot not provided."
].randomChoice());
</script>Untested.

mburt
04-16-2007, 10:18 PM
Got an error, and it's saying that Twey.Pyth
onic isn't an object, or has no properties.
But this will work:

<script type="text/javascript">
Array.prototype.randomChoice = function() {
return this[Math.floor(Math.random() * this.length)];
};
document.images['banner'].src = "http://www.a2h.8m6.net/images/logo.php?msg=" +
encodeURIComponent([
"now with 10&#37; more potatoes!",
"anger2headshot is not a crack dealer.",
"here is your free complementary wastage of your internet bandwidth...",
"CHUCK NORRIS IS BEHIND YOU!!!",
"free lying parrot not provided."
].randomChoice());
</script>

Twey
04-16-2007, 10:24 PM
Whoops, name difference left over from debugging. Fixed now, but yeah, that'll work just as well.

mburt
04-16-2007, 11:37 PM
BTW, that's some complex script you have going there. How do you call functions with string names?
example:

'reduce' : function(f) {
if(this.length === 0)
return null;
for(var i = 1, c = this[0]; i < this.length; ++i)
c = f(c, this[i]);
return c;
},
Twey.pythonic["reduce"](); ?
Never used anything like that before.

Twey
04-17-2007, 05:13 PM
Twey.pythonic["reduce"](); ?Twey.Pythonic.Array.reduce() would call the function. It still wouldn't work, though, since it's meant to be called as a property of an array, which is what the LOAD() function is for. The end result is a fairly neat way (I think) of avoiding cluttering up native prototypes, especially since that file contains most of the functions I find myself writing from scratch for scripts, so in most cases only one or two will actually be used.

riptide
04-17-2007, 05:51 PM
This switch stuff seems to be a big problem. but you would think that it would work better with the case part like a big if else, but still...