Control Flow Flashcards Preview

JavaScript Basics > Control Flow > Flashcards

Flashcards in Control Flow Deck (15)
Loading flashcards...
1

Review: Control Flow

if/else statements make binary decisions and execute different code based on conditions.
All conditions are evaluated to be truthy or falsy.
We can add more conditional statements to if/else statements with else if.
switch statements make complicated if/else statements easier to read and achieve the same result.
The ternary operator (?) and a colon (:) allow us to refactor simple if/else statements.
Comparison operators, including , <=, and >= can compare two variables or values.
After two values are compared, the conditional statement evaluates to true or false.
The logical operator && checks if both sides of a condition are truthy.
The logical operator || checks if either side is truthy.
The logical operator !== checks if the two sides are not equal.
An exclamation mark (!) switches the truthiness / falsiness of the value of a variable.
One equals symbol (=) is used to assign a value to a variable.
Three equals symbols (===) are used to check if two variables are equal to each other.

2

if / else if / else syntax

let needsCoffee = true;
if (needsCoffee === true) {
console.log('Finding coffee');
} else {
console.log('Keep on keeping on!');
}

3

switch statements syntax

let groceryItem = 'papaya';

switch (groceryItem) {
case 'tomato':
console.log('Tomatoes are $0.49');
break;
case 'lime':
console.log('Limes are $1.49');
break;
case 'papaya':
console.log('Papayas are $1.29');
break;
default:
console.log('Invalid item');
break;
}

4

random number between 0 & 5

let randomNumber = Math.floor(Math.random() * 6);

5

What will the following code log to the console?

let runTime = 35;
let runDistance = 3.5;
if (runTime <= 30 && runDistance > 3.5) {
console.log("You're super fast!");
} else if (runTime >= 30 && runDistance <=3) {
console.log("You're not making your pace!");
} else if (runTime > 30 || runDistance > 3) {
console.log('Nice workout!');
} else {
console.log('Keep on running!');
}
What will the following code log to the console?

Keep on running!

You're super fast!

Nice workout!

You're not making your pace!

Nice workout!

6

What will the following code log to the console?

let needTacos = true;

if (needTacos) {
console.log("Finding tacos");
} else {
console.log("Keep on keeping on!");
}
What will the following code log to the console?

Finding tacos

Keep on keeping on!

Finding tacos
Correct! Since needTacos is true, the first block will run.

7

Which of the following operators checks if two variables are equal?

Which of the following operators checks if two variables are equal?

===

==

=

===

8

Which of the following blocks of code will execute?

let isHungry = false;
let moneyInPocket = 14;

if (isHungry && moneyInPocket > 10) {
console.log("Let's go out for lunch!");
} else if (!isHungry && moneyInPocket > 10) {
console.log("Save that money for later!");
} else if (isHungry && moneyInPocket < 10) {
console.log('Eat something at home');
} else {
console.log('Save up for when you are hungry!');
}

console.log("Save that money for later!");

9

Which of the following variables contains a truthy value?

Which of the following variables contains a truthy value?

let varFour = '';

let varOne = "false";

let varTwo = false;

let varThree = 0;

let varOne = "false";

10

What will the following code log to the console?

let weather = "spring";
let clothingChoice = "";

if (weather === "spring") {
clothingChoice = "You need a light jacket, and possibly an umbrella.";
} else if (weather === "summer") {
clothingChoice = "Make sure to take your sunscreen.";
} else if (weather === "fall") {
clothingChoice = "Wear a light jacket.";
} else {
clothingChoice = "Wear a heavy coat.";
}
console.log(clothingChoice);

You need a light jacket, and possibly an umbrella.

11

What will the following code log to the console?

let groceryItem = "apple";

switch (groceryItem) {
case "tomato":
console.log("Tomatoes are $0.49");
break;
case "lime":
console.log("Limes are $1.49");
break;
case "papaya":
console.log("Papayas are $1.29");
break;
default:
console.log("Invalid item");
break;
}

Invalid item
Correct! Since groceryItem = "apple", it does not match any of the cases, so the default block will run.

12

What is the logical operator for "both of these things must be true" ?

What is the logical operator for "both of these things must be true" ?

||

!

&&

&&

13

How would you properly refactor this code using the ternary operator?

if (walkSignal === 'Walk') {
console.log('Walk!');
} else {
console.log('Do Not Walk!');
}

walkSignal === 'Walk' ? console.log('Walk!') : console.log('Do Not Walk!');

14

All variables that have been created and set are truthy (and will evaluate to true if they are the condition of a control flow statement) unless they contain one of the seven values listed:

false
0 and -0
"" and '' (empty strings)
null
undefined
NaN (Not a Number)
document.all (something you will rarely encounter)

15

Using a Ternary expression
Check if UserName true + Say Hello

userName ? console.log('Hello ' + userName) : console.log('Hello!');