• Lucid Dreaming - Dream Views




    Results 1 to 7 of 7
    1. #1
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90

      Javascript Help Please!

      I have a snippet of code:

      PHP Code:
      switch (id)
      {
          case 
      1:
              
      doFunction(parameter1);
              break;
          case 
      2:
              
      doFunction(parameter2);
              break;
          case 
      3:
              
      doFunction(parameter3);
              break;
          default:
              
      document.write('Error: '+id);

      The strange thing is that the document says, "Error: 3". If the variable id is equal to 3, then why didn't the switch statement go through case 3? I must have something confused.

      Help anyone?
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    2. #2
      Member Achievements:
      1 year registered 1000 Hall Points Veteran First Class
      SomeDreamer's Avatar
      Join Date
      Jul 2007
      Gender
      Location
      Denmark
      Posts
      670
      Likes
      44
      hmm... what kind of data type is your ID variable? There doesn't seem to be any specific errors within the snippet you provided. Since it doesn't find any match within the switch statement my only guess would be that the data type you try to match with the cases are different.

    3. #3
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      I'd agree with SomeDreamer. Show more of the JS

    4. #4
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Maybe the 3 you give it is a string or a character. Try to add
      Code:
      id = parseInt(id);
      before the switch statement.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    5. #5
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      Quote Originally Posted by khh View Post
      Maybe the 3 you give it is a string or a character. Try to add
      Code:
      id = parseInt(id);
      before the switch statement.
      Thanks a lot! That did the trick
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    6. #6
      Member Achievements:
      1 year registered 1000 Hall Points Veteran First Class
      SomeDreamer's Avatar
      Join Date
      Jul 2007
      Gender
      Location
      Denmark
      Posts
      670
      Likes
      44
      Thanks a lot! That did the trick
      Basically the same thing as mentioned by me then. Don't know if it should have been explained better, but the idea is that the switch variable you use need to match the same data type as the case values. So in this example you used a string and tried to compare it to an integer type (case 1, etc).

      It's also allowed to change the switch statement to the likes of the data type you're comparing it with, like:
      switch (id)
      {
      case "1":
      doFunction(parameter1);
      break;
      case "2":
      doFunction(parameter2);
      break;
      case "3":
      doFunction(parameter3);
      break;
      default:
      document.write('Error: '+id);
      }
      When embedded in quotes, it's now taken as a string, and your code would also have worked. Obviously this solution isn't as pretty converting the switch variable before comparing But gives you the general idea.

    7. #7
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      @SomeDreamer: Yeah, I just forgot that id was of type String. Thanks for the help!
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    Bookmarks

    Posting Permissions

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