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
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.
26
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'.
27
What is an infinite loop?
An infinite loop keeps running forever.
28
What helps you exist a loop?
Break helps you exit a loop.
29
Why is this example an infinite loop?
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
What happens if you create an infinite loop?
31
How do you prevent infinite loops?
Establish a condition that eventually becomes false.
32
What is the consequence of an infinite loop?
Infinite loops crash browser or computer.
33
Why is this an infinite loop?
The counter will always be greater than 0. This is why it's an infinite loop.
34
Please fill in the correct answer Consider the following console output: #1 #3 #5 #9 #11 #13 #15 Complete the do… while loop to display the above output to the console:
35
Given the code below, what will display in the console?
Hello Goodbye
36
Do you always need to use a counter to specify an exact number of times a loop must run?
37
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
++
38
Given the code below, what will print to the console?
The condition is false, the loop never runs and Hello never prints.
39
Why is this foor loop shorter?
It has 2 lines of code, instead of 3.
40
What does the counter do?
The counter is evaluated before the loop begins.
41
Is the counter condition evaluated before the loop begins each time?
Yes The counter needs to be below 10 each time before the loop begins. counter \< 10;
42
How is the counter being updated with each loop iteration?
counter +=1;
43
How would you incorporate the increment operator into this example?
Use counter ++;
44
Why would you use letters for variables?
Make loops even easier to write with single letters than whole words
45
What does the letter i generally substitute in the loop?
i replaces counter mentions in the loop you can use letters for counter variables
46
Aside from i, what other letter is commonly used as a single letter variable name?
J
47
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.
48
What is the outcome produced by this loop?
It produces 10 divs.
49
Why is the greater than or equal to sign important?
It ensures that the numberes in the div is truly 1 through 10, rather than 1 through 9.
50
Can the counter iterate by different multiples? Or is it stuck to just 1?
You can do multiple of 5s or any number by changing the skip pattern
51
What's the multiple by which the iteration moves in this for loop?
Increases by 5 i += 5
52
What are the two ways to exit a loop?
It can be done through counter conditions. It can be done through adding a break statement.
53
What is a break statement?
The break statement immediately exits the loop, when encountered inside a loop.
54
In this example, when is the break statement applied?
The break is triggered when the guess matches the secretWord
55
Does code after the break statement run?
The break statement works like a return statement. Any code inside the loop that appears after the break statement will never run
56
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).
57
What does refractor mean?
It means to make code more succinct.
58
What does D.R.Y. mean?
Don't repeat yourself
59
Why is D.R.Y. one of the most important concepts a programmer can learn?
Reduces repetition of code
60
How is D.R.Y. code beneficial?
Easier to read Easier to maintain
61
What's a tell-tell sign you need to use refractoring?
When you have copied/pasted code again and again or you have lots of repetitive sets-- time to use a loop to condense
62
Consider the following code example Why is this NOT an infinite loop.
It eventually ends when the counter is less than 100.
63
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:
for 10
64
Which word forces the JS engine to exist a loop even when the loop condition is still true?
break
65
How many times will the following loop run?
Actually 11 BEcause it starts at 0, not 10.
66
What's the shorthand version of the following code?
67
What causes an infinite loop?
When the loop’s condition never evaluates to false.
68
Refractor the code to be more concise
69