#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Switches in Javascript


    Hello! This is my first post so I'd like to start by thanking the team here at Dev Shed for their work, and I'd also like to thank the open source community as well for all the help I'll undoubtedly receive along the way.

    With that being said, I just wrote my very first switch in javascript and it looks like so...

    Code:
    var die=Math.ceil(Math.random()*6);
    var output="";
    switch (die){
    	case 1:
    	output="I";
    	break;
    	case 2:
    	output="II";
    	break;
    	case 3:
    	output="III";
    	break;
    	case 4:
    	output="IV";
    	break;
    	case 5:
    	output="V";
    	break;
    	case 6:
    	output="VI";
    	break;
    	default:
    	output = "PROBLEM";
    }
    alert(output);
    The good news is that the code works fine.

    I pulled the code straight out of Javascript for dummies and the final line of code
    Code:
    alert(output);
    wasn't in the book. I had some trouble figuring out how to make the code show up in the browser but figured it out after a few minutes.


    My question is... Do switches automatically generate client-side alerts? Or was there an error in the writing of the book?

    From what I've gathered in the book, the author would lead me to believe that my last piece of code wasn't necessary, and the alert would automatically be generated, but after trying it in 4 browsers i can say with certainty that the code wouldn't generate an alert until after i amended it.

    My reason for asking is that I want to ensure i have a thorough knowledge of exactly what why and how i'm learning web development.

    Thanks,
    Vin
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    You are correct: the browser won't show any alert()s unless you specifically tell it to show one.

    Printed books often have mistakes. Take what they say with a grain of salt and independently verify what they tell you. Also check their website(s) for errata sections: there may be mistakes but they'll often address them after publishing.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0
    I figured as much...

    Thank you requinix for the information!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    It's also possible they weren't going to output anything until later in the chapter. People get confused when you say:

    "Now you're going to output it to make sure it works. I know it WILL work, since I wrote it and tested it before I gave it to you, but I'm trying to teach you good debugging. No, there's no bugs. Just alert it. See? Now delete it. The alert. No not the whole thing. Because we needed the alert for debugging. No, of course there's no bugs in THIS, but in the future...No, bugs won't magically appear in this code in the future, I meant in YOUR future. You know what, just move on."
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0
    Wait? So there's a bug in my code?

    Lol jk jk

IMN logo majestic logo threadwatch logo seochat tools logo