Chapter 4: Decisions and Loops Flashcards

1
Q

What happens in an if / else statement

A

A condition in ( ) is checked. If its true, the code that immediately follows the condition in { } is run. If not the code preceded by else in { } is run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What  do the following comparison operators do:
==
!=
===
!==
>
=
A

== is equal to
!= not equal to
=== strict equal to (also checks the data type as well as the value)
!== strict not equal to (also checks the data type as well as the value)
> greater than
= greater than or equal to

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

In general how does a condition work for say we have 2 variables

A

Usually there is 2 operands and an operator. The operas are placed on either side of the operator. They can be value, expressions (remember these result in a single value) or variables and you’ll often see them enclosed in ( )

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

Can we use expressions as an operand?

A

Yes

e.g. (score1 + score2)

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

What are logical operators

A

The and, or, not. They allow us to compare the results of more then one comparison operator.

&& and
|| or
! not

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

What’s short circuit evaluation?

A

If a minimal conditions are met then any subsequent conditions are ignored.

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

What’s an if statement

A

It checks if a condition is true. If it is, it runs the code

if (score >= 50) {
congratulate();
}

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

What’s an if…else statement?

A

It specifies some code if a condition is true, or if not, some other code to run

if (score >= 50) {
congratulate();
} else {
failure();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s a switch statement? Write out the basic syntax

A

This allows you specify what the code should do if a particular value is true.

switch(level) {
case ‘One’:
title = ‘Level 1’;
break;

case ‘Two’:
title = ‘Level 2’;
break;

case ‘Three’:
title = ‘Level 3’;
break;

default:
title = ‘Test’;
break;
}

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

When should I use a switch or an if?

A

When a series of if’s are used, they are all checked even if a match has been found (so it performs more slowly then switch)

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

What is type coercion?

A

JS can convert data types behind the scenes to complete an operation

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

What does Javascript is weakly typed mean?

A

It means the data type of a variable can change (as opposed to a strongly typed language).

That’s why its a good idea to use strict equals and not equals (check if type coercion has occurred).

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

Every value in javascript can be treated as true or false because of type coercion. True or false?

A

True

The following values are considered as if they were false:

false boolean, number zero, NaN, empty value, variable with no value assigned

the following values are considered as if they were true:

boolean true, all numbers other than zero, strings with content, calculations, true written as a string, zero written as a string, false written as a string

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

When would you want to use a for loop?

A

When you want to run a loop a specific amount of times

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

When would you use a while loop?

A

You’re not sure how many times the loop needs to run. The condition is not a counter.

A variation of the while loop is the do…while. Main difference is the code in a do…while is run at least once.

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

What are the three parts of a for loop counter?

A

counter, condition, increment

17
Q

What does the break keyword do?

A

Exit out of the loop and go to the next statement.

18
Q

What doe the continue keyword do?

A

keep doing the current iteration, and then check the condition again.

19
Q

Why would you want to make sure any variable that can be defined outside of the loop and is not changed in the loop is?

A

If it is declared in the loop, it would be recalculated each time, wasting resources.