Loops Flashcards

1
Q

What is a loop?

A

A loop is a snippet of programming that repeat actions, in specific conditions, until a condition is true.

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

What are examples of a loop?

A

Repeat same set of actions, over and over again

Examples:

  • create a list of 1000 numbers
  • display 20 photos
  • repeat prompt dialog until correct answer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens without using loops?

A

Without loops, you have to write code manually.

No big deal when it’s a small amount, but it is repetitive.

It’s harder for fellow developers.

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

Why use loops?

A

Loops help automate code.

It’s better to automate code than duplicate code.

Repetitive code doesn’t scale well for 100 or 1000 times.

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

What analogy can you make to compare the concept of loops to understand what it does?

A

Imagine you’re running a marathon, 26.2 miles

Path 1 is random, unpredictable, confusing, inefficient

Path 2 is a well-defined loop, consistent, predictable

The loop is like the second path efficient & effective.

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

What are the 3 main types of loops?

A

While

Do While

For Loop

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

What is the syntax for the while loop?

A

while () {

}

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

What is the tell-tell sign you should use a loop?

A

When you have duplicates, copy/paste, or repetition of manually written code, then it makes sense to create a loop to write efficient code.

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

Explain the logic behind this while loop example

line for line

A

the function generates the random number

lap counter, at the start it’s 0

the console.log prints the message with a random number.

Each time the loop runs, it adds 1 to the counter

JS interpreter asks “Is the counter less than 10?”

It runs again and again.

The counter runs 10 times, producing 10 random numbers.

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

What is the do while syntax?

A

do {

} while ( );

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

What’s the difference between the Do Loop and Do While Loop?

A

The do while will always execute code, at least once, before the condition is checked

The loop will check the condition first, before running the code.

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

When should you use a loop?

A

When you want to repeat the same set of
actions a certain number of times.

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

Fill in the blanks provided below

A

let

while

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

How many times does a loop repeat?

A

A loop repeats an action over and over again, or a given number of times, as long as a test condition evaluates to true.

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

Please fill in the correct answer in each blank provided below.

Complete the code below so that the loop runs 10 times.

A

counter < 10

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

Why will the following loop never run?

A

The variable num begins with the value of 0.

The condition asks if that variable is greater than 20.

0 is not greater than 20, so the loop never runs.

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

While loop runs a block of code before checking the condition?

A

Do While

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

Enter the correct answer in each blank provided in photo

A

do

while

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

What is an increment operator?

A

An increment operator increases a number value by 1

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

How do you use the increment operator?

A

Use the increment operator by replacing
+=1 with two plus symbols (++)

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

Where is the increment operator in this example?

A

counter++;

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

What’s the logic behind the increment operator?

A

It’s like using the addition operator and adding 1,

the incrementor operator adds 1 to the current number stored in the counter and returns a value each time through the loop.

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

What does the decrement operator do?

(–)

A

It subtracts 1 from an integer value.

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

What happens in this example with the decrement operator?

A

This time, the loop counts down from 10 to 0.

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

Create a while loop that logs the current value of count to the console 26 times.

Use the count variable to track the number of times the loop runs.

Don’t forget to increment count by one each time through the loop.

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

You don’t always need to use a counter or specify an exact number of times that a loop must run. All you need is a condition that evaluates to false at some point so that the loop can end.

The code in script.js opens a prompt dialog that asks for a password and assigns it to the variable secret. It also displays an alert dialog. Currently, no code checks the password.

Add a do…while loop that keeps displaying the prompt dialog until the user types ‘sesame’.

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

What is an infinite loop?

A

An infinite loop keeps running forever.

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

What helps you exist a loop?

A

Break helps you exit a loop.

29
Q

Why is this example an infinite loop?

A

The counter starts at 0 and increases each time through the loop. The counter will always be greater than or equal to 0, the condition will ALWAYS be true. This is why it is an infinite loop.

30
Q

What happens if you create an infinite loop?

A
31
Q

How do you prevent infinite loops?

A

Establish a condition that eventually becomes false.

32
Q

What is the consequence of an infinite loop?

A

Infinite loops crash browser or computer.

33
Q

Why is this an infinite loop?

A

The counter will always be greater than 0. This is why it’s an infinite loop.

34
Q

1

Please fill in the correct answer

Consider the following console output:

Complete the do… while loop to display the above output to the console:

A
35
Q

Given the code below, what will display in the console?

A

Hello Goodbye

36
Q

Do you always need to use a counter to specify an exact number of times a loop must run?

A
37
Q

Please fill in the correct answer in each blank provided below

Complete the code including shorthand operator that increase the counter by one each time through the loop

A

++

38
Q

Given the code below, what will print to the console?

A

The condition is false, the loop never runs and Hello never prints.

39
Q

Why is this foor loop shorter?

A

It has 2 lines of code, instead of 3.

40
Q

What does the counter do?

A

The counter is evaluated before the loop begins.

41
Q

Is the counter condition evaluated before the loop begins each time?

A

Yes

The counter needs to be below 10 each time before the loop begins.

counter < 10;

42
Q

How is the counter being updated with each loop iteration?

A

counter +=1;

43
Q

How would you incorporate the increment operator into this example?

A

Use counter ++;

44
Q

Why would you use letters for variables?

A

Make loops even easier to write with single letters than whole words

45
Q

What does the letter i generally substitute in the loop?

A

i replaces counter mentions in the loop

you can use letters for counter variables

46
Q

Aside from i, what other letter is commonly used as a single letter variable name?

A

J

47
Q

Create a for loop that logs the numbers 5 to 100 to the console. Use the console.log() method to log a value to the console.

A
48
Q

What is the outcome produced by this loop?

A

It produces 10 divs.

49
Q

Why is the greater than or equal to sign important?

A

It ensures that the numberes in the div is
truly 1 through 10, rather than 1 through 9.

50
Q

Can the counter iterate by different multiples? Or is it stuck to just 1?

A

You can do multiple of 5s or any number by changing the skip pattern

51
Q

What’s the multiple by which the iteration moves in this for loop?

A

Increases by 5

i += 5

52
Q

What are the two ways to exit a loop?

A

It can be done through counter conditions.

It can be done through adding a break statement.

53
Q

What is a break statement?

A

The break statement immediately exits the
loop, when encountered inside a loop.

54
Q

In this example, when is the break statement applied?

A

The break is triggered when the guess matches the secretWord

55
Q

Does code after the break statement run?

A

The break statement works like a return statement.

Any code inside the loop that appears after the break statement will never run

56
Q

The for loop in script.js runs as many times as the length value of the string assigned to the message variable. It logs the current value of i to the console, and the rest of the program continues when the loop completes.

Add the statement that immediately terminates the for loop if the value of i is equal to message / 2 (half the length value).

A
57
Q

What does refractor mean?

A

It means to make code more succinct.

58
Q

What does D.R.Y. mean?

A

Don’t repeat yourself

59
Q

Why is D.R.Y. one of the most important
concepts a programmer can learn?

A

Reduces repetition of code

60
Q

How is D.R.Y. code beneficial?

A

Easier to read

Easier to maintain

61
Q

What’s a tell-tell sign you need to use refractoring?

A

When you have copied/pasted code again and again or

you have lots of repetitive sets– time to use a loop to condense

62
Q

Consider the following code example

Why is this NOT an infinite loop.

A

It eventually ends when the counter is less than 100.

63
Q

Please fill the correct answer in each blank space provided below

Complete the code below to create a for loop that executes 10 times, logging the value of the variable i to the console each time:

A

for

10

64
Q

Which word forces the JS engine to exist a loop even when the loop condition is still true?

A

break

65
Q

How many times will the following loop run?

A

Actually 11

BEcause it starts at 0, not 10.

66
Q

What’s the shorthand version of the following code?

A
67
Q

What causes an infinite loop?

A

When the loop’s condition never evaluates to false.

68
Q

Refractor the code to be more concise

A
69
Q
A