...

View Full Version : Javascript switch statement



digic25
05-07-2006, 06:49 PM
How would I go about switching this script to a switch statement, with the generated random value (1-10) displaying the correct website.

Any help is appreciated.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script type="text/javascript">
function isRandom() {
for(i=0;i<=10;i++) {
var value = Math.random()*10; //generates a random number between 1 and
10.
value = Math.floor(value); //converts value to a whole number.
if (value%2 == 0){
Random = value;
}
}
document.write(n_2 + " " +"http://www.msn.com"+"<br>");
document.write(n_4 + " " +"http://www.yahoo.com"+"<br>");
document.write(n_6 + " " +"http://www.monster.com"+"<br>");
document.write(n_8 + " " +"http://www.hotjobs.com"+"<br>");
document.write(n_1 + " " +"http://www.google.com"+"<br>");
document.write(n_3 + " " +"http://www.google.com"+"<br>");
document.write(n_5 + " " +"http://www.google.com"+"<br>");
document.write(n_7 + " " +"http://www.google.com"+"<br>");
document.write(n_9 + " " +"http://www.google.com"+"<br>");
document.write(n_10 + " " +"http://www.google.com"+"<br>");
}
</script>
</head>
<body>
<script type="text/javascript">

isRandom();

</script>
</body>
</html>

digic25
05-07-2006, 07:44 PM
I tried to complete the script myself, but I'm unsure of how to make all other numbers besides 2,4,6,and 8 to go to http://www.google.com or even get it to run.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
<script type="text/javascript">
var n = new Number()
randomNumber=n.getNumber()
switch (randomNumber)
{
case 2:
document.write("<b>http://www.msn.com</b>")
break
case 4:
document.write("<b>http://www.yahoo.com</b>")
break
case 6:
document.write("<b>http://www.monster.com</b>")
break
case 8:
document.write("<b>http://www.hotjobs.com</b>")
break
default:
document.write("<b>http://www.google.com</b>")
}
</script>


</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum