More on Control Flow in JS

I The Story So Far: If, Else, and Loops

i. If / else 1/14

var isEven = function(number) {
    if (number % 2 === 0) {
        return true; 
        }
    else {
        return false;
    }
};

ii. If / else if / else 2/14

for (var counter = 1; counter < 11; counter++) {
	console.log(counter);
}

iii. For or while 3/14

var count = 2
 while (count < 10) {
    console.log("while loop again!");
    count++;
}

iv. Sneak preview: the switch statement 4/14
Save and submit

II Introducing the Switch Statement
i. Adding to an existing switch 5/14

var color = prompt("What's your favorite primary color?","Type your favorite color here");

switch(color) {
  case 'red':
    console.log("Red's a good color!");
    break;
  case 'blue':
    console.log("That's my favorite color, too!");
    break;
  case 'yellow':
    console.log("I don't like yellow");
    break;
  default:
    console.log("I don't think that's a primary color!");
}

ii. Practice with switch 6/14

var candy = prompt("What's your favorite candy?","Type your favorite candy here.");

switch(candy) {
  case 'licorice':
    console.log("Gross!");
    break;
  case 'gum':
    console.log("I like gum!");
    break;
  case 'beets':
    console.log("...is that even a candy?");
    break;
  default:
    console.log("We don't have this one");
}

iii. More practice with switch 7/14

var answer = prompt("Which is your favorite city?");

switch(answer) {
  case 'New Delhi':
    console.log("That's my city!");
    break;
  case 'Dubai':
    console.log("Are you a shopaholic?");
    break;
  case 'Beijing':
    console.log("So, you know Mandarin too?");
    break;
  default:
    console.log("Oops! not in this list!");
}

iv. All on your own 8/14

var flower = prompt("Which flower do you love?");

switch(flower) {
    case 'rose':
        console.log("Good choice");
        break;
    case 'jasmine':
        console.log("Lovely!");
        break;
    case 'sunflower':
        console.log("I love it");
        break;
    default:
        console.log("Never heard of this one.");
}

v Mid-lesson breather 9/14

var flower = prompt("Which flower do you love?");

switch(flower) {
    case 'rose':
        console.log("Good choice");
        break;
    case 'jasmine':
        console.log("Lovely!");
        break;
    case 'sunflower':
        console.log("I love it");
        break;
    default:
        console.log("Never heard of this one.");
}

III Logical Operators

i. Overview 10/14

var iLoveJavaScript = true;
var iLoveLearning = true ;

if(iLoveJavaScript && iLoveLearning) {
  // if iLoveJavaScript AND iLoveLearning:
  console.log("Awesome! Let's keep learning!");
} else if(!(iLoveJavaScript || iLoveLearning)) {
  // if NOT iLoveJavaScript OR iLoveLearning:
  console.log("Let's see if we can change your mind.");
} else {
  console.log("You only like one but not the other? We'll work on it.");
}

ii. And 11/14

var hungry = true;
var foodHere = true;
var eat = function() {
    if ( hungry && foodHere ) {
        return true;
    }
    else { 
        return false;
    }
};

iii. Or 12/14

var bored = true;
var tired = false; 
var nap = function() {
    if ( bored || tired ) {
        return true;
    }
    else {
        return false;
    }
};

iv. Not 13/14

var programming = !true;

var happy = function() {
    if (programming === false) {
        return true;
    }
    else {
        return false;
    }
};

v Review 14/14
Save and Submit

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s