www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Need some help converting this if statement to switch statement

  1. #1
    Join Date
    Nov 2009
    Posts
    37

    resolved [RESOLVED] Need some help converting this if statement to switch statement

    Hey guys,

    I need some help converting this if statement to a switch statement script.
    I tried but I messed up in the variable and the cases. I know that I mess up in something simple again

    Any help will be very appriciated, thanks in advance

    Code:
    <html>
    <head>
    <script type="text/javascript">
       n = Math.floor(Math.random()*11)
      if(n == 2){document.write("<a href='http://www.facebook.com/'>Facebook</a>");}
      else if(n == 4){document.write("<a href='http://www.myspace.com/'>MySpace</a>");}
      else if(n == 6){document.write("<a href='http://www.bing.com/'>Bing</a>");}
      else if(n == 8){document.write("<a href='http://www.google.com/'>Google</a>");}
      else{document.write("<a href='http://www.mysite.com/'>MySite</a>");}
    </script>
    <body>
    </body>
    </head>

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    what did you try?

    Eric

  3. #3
    Join Date
    Mar 2007
    Posts
    946

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Lightbulb

    Try this:
    Code:
    <html>
    <head>
    <title>Random Link</title>
    <script type="text/javascript">
      n = Math.floor(Math.random()*11);
      switch (n) {
        case 2 : document.write("<a href='http://www.facebook.com/'>Facebook</a>"); break;
        case 4 : document.write("<a href='http://www.myspace.com/'>MySpace</a>"); break;
        case 6 : document.write("<a href='http://www.bing.com/'>Bing</a>"); break;
        case 8 : document.write("<a href='http://www.google.com/'>Google</a>"); break;
        default: document.write("<a href='http://www.mysite.com/'>MySite</a>"); break;
      }
    </script>
    </head>
    <body>
    </body>
    </html>
    Look at <HTML> tag pairing order in your post compared to this.

  5. #5
    Join Date
    Nov 2009
    Posts
    37
    Thanks a lot guys that did it!

    I was messing up to the switch (n) and the default case.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles