Standard Exercises

0. Try to determine the outcome of the following statements and then enter them into the console to see the answers.

var age = 21;

if ( age > 18 ) {
    console.log("You can vote!");
}
var age = 15;

if ( age > 18 ) {
    console.log("You can vote!");
} else { 
    console.log("You cannot vote!");
}

1. For what values does the following code print "YES"?

if ( (age > 21) && (age < 50) && 
     (gender == "Female") ) {
   console.log("YES");
}

2. Write an if statement that checks whether the bool likesCoffee is true and prints "Good Choice" if it is.

var likesCoffee = ...;

if ( ... ) {
	console.log("Good Choice");
}

3. Write an if statement that checks whether the name is Alex. If it is - print "Hello Alex" to the console. Experiment with different values for 'name' to ensure your code works.

var name = ...;

if ( ... ) {
	console.log( ... );
}

4. Write an if, else statement that checks whether age is 21 or greater. If it is, print "You are old enough to drink in America!" otherwise print "Bad luck buddy."

5. You can use the NOT operator to inverse a boolean (or a boolean expression). What do you think the following will print? Try it to find out.

var likesCoffee = true;
console.log( !likesCoffee );

6. Similarly, you can apply the not operator to the result of a boolean expression to invert it. What do you think the following will print? Try it to find out!

var likesCoffee = true;

if ( !likesCoffee ) {
	console.log( "Boo!" );
} else {
	console.log( "Yay!" );
}

7. We're going to determine whether a theme park visitor is able to go on a specific ride. If the rider is 18 or over, or their height is over 140cm then they are allowed to ride.

var age = ...;
var height = ...;

...

8. Based on the user's height, we want to suggest which ride they should go on. If the user is over 180cm we should recommend that they go on the "Thunder Strike Coaster". If they are under 180cm but over 150cm, we should recommend they go on the "Twisting Dragons" ride. Otherwise, we should recommend that they go on the "Flower Cups" ride.

Advanced Exercises

Note. Something we don't touch upon during this course is the switch statement. The switch statement allows you replace long if/then/else statements that are checking for a number of set conditions. You can read about switch statements here.

9. Use a switch statement to print out the cost of the given drink;

// coffee costs $2, tea costs $1 and juice costs $2.50
var drink = "coffee"; // covfefe?

switch( ... ) {
	case ...: 
		...
		break;
	case ...:
		...
		break;
}

Note. Another useful features in JavaScript for writing concise code is the ternary operator. It acts as a shorthand for an if statement. You can read about the ternary operator here.

The ternary operator is useful for setting variables based on a condition without a full if,then,else statement. An example is shown below;

var a = 10;
var b = (a > 5) ? "A is high" : "A is low";

10. Use the ternary operator to replace the if statement in the following code;

 
var age = 22;

if ( age >= 21 ) { 
	var result = "Can Vote";
} else {
	var result = "Cannot Vote";
}

11. Use the ternary operator to set a value depending on whether the given string is longer than 10 characters long.

var name = "John W";
var isLongerThan10Letters = ...;
console.log("Hello World");

    
Hint: Alternatively, just hold "shift" and press "enter" to run your code.