Control Flow Flashcards

1
Q

Review: Control Flow

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

if / else if / else syntax

A
let needsCoffee = true;
if (needsCoffee === true) {
    console.log('Finding coffee');
} else {
    console.log('Keep on keeping on!');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

switch statements syntax

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

random number between 0 & 5

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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!

A

Nice workout!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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!

A

Finding tacos

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

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

===

==

=

A

===

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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!’);
}

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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;

A

let varOne = “false”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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);

A

You need a light jacket, and possibly an umbrella.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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;
}
A

Invalid item

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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” ?

||

!

&&

A

&&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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:

A
false
0 and -0
"" and '' (empty strings)
null
undefined
NaN (Not a Number)
document.all (something you will rarely encounter)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Using a Ternary expression

Check if UserName true + Say Hello

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly