Monday, March 10, 2014

Javascript

I think I've finished everything for the week so I have moved on to the next lesson in codeacademy, I'll come back and edit my notes if needed.

  • The switch statement is very useful, it's a substitute for else if and allows the user to preset as many cases as they like and if one of the options is chosen it will execute the code otherwise it will execute some default code.
    • In code academy they have                                                                        var lunch = prompt("What do you want for lunch?","Type your lunch choice here");

      switch(lunch){
        case 'sandwich':
          console.log("Sure thing! One sandwich, coming up.");
          break;
        case 'soup':
          console.log("Got it! Tomato's my favorite.");
          break;                                                                                                       default:
          console.log("Huh! I'm not sure what " + lunch + " is. How does a sandwich sound?"); 
                                                                                         From what it looks like the user is prompted the questions what they want for lunch and if they choose either sandwich or soup they will get a preset response otherwise they will get the last message because they did not choose any of the available options.

1 comment:

  1. A standard C style switch statement. It is funny that all the C like languages (including Java and JavaScript) chose to keep the rather unfortunate requirement to require a break in each case. That's how C did it, and they all just went along...

    ReplyDelete