Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript switch statement

    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>
    Last edited by digic25; 05-07-2006 at 06:57 PM.

  • #2
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •