1
00:00:02,180 --> 00:00:03,880
Now, the last kind of loop

2
00:00:03,880 --> 00:00:07,470
I mentioned on the slide was a while loop.

3
00:00:07,470 --> 00:00:09,180
And with the for loops,

4
00:00:09,180 --> 00:00:13,230
we basically always had like a fixed number of times

5
00:00:13,230 --> 00:00:14,510
that loop executed

6
00:00:14,510 --> 00:00:16,320
either because we defined it here

7
00:00:16,320 --> 00:00:19,210
or because we had a fixed amount of array elements

8
00:00:19,210 --> 00:00:21,143
or properties in an object.

9
00:00:22,080 --> 00:00:24,850
With while, we instead just have a condition

10
00:00:24,850 --> 00:00:26,400
on which we depend.

11
00:00:26,400 --> 00:00:30,370
And as long as that condition is true, we continue.

12
00:00:30,370 --> 00:00:32,313
Now, let me show you an example.

13
00:00:33,400 --> 00:00:37,040
We write a while loop by using the while keyword,

14
00:00:37,040 --> 00:00:41,870
then parentheses between which we can put our condition,

15
00:00:41,870 --> 00:00:43,563
and then curly braces.

16
00:00:44,410 --> 00:00:46,840
As long as the condition we put in here is met,

17
00:00:46,840 --> 00:00:49,680
we'll continue executing this code.

18
00:00:49,680 --> 00:00:52,060
So if I would write while true here,

19
00:00:52,060 --> 00:00:54,590
we would create an infinite loop

20
00:00:54,590 --> 00:00:57,410
and therefore for you should not saved this

21
00:00:57,410 --> 00:00:59,900
because this will crash your website

22
00:00:59,900 --> 00:01:01,453
since it will never end.

23
00:01:02,580 --> 00:01:05,500
Instead, we could be doing something like this.

24
00:01:05,500 --> 00:01:07,400
We could add a variable, "isFinished,"

25
00:01:09,780 --> 00:01:12,370
and that could store a Boolean value,

26
00:01:12,370 --> 00:01:14,980
so initially it could be "false."

27
00:01:14,980 --> 00:01:16,090
Here in the while loop,

28
00:01:16,090 --> 00:01:17,880
we then check for the opposite.

29
00:01:17,880 --> 00:01:21,350
We want to keep on running as long as we are not finished,

30
00:01:21,350 --> 00:01:24,723
hence we can execute "not isFinished" here.

31
00:01:26,530 --> 00:01:27,480
And in here,

32
00:01:27,480 --> 00:01:32,150
we could now show a confirmation dialogue to the user

33
00:01:32,150 --> 00:01:35,570
with help of the confirm function.

34
00:01:35,570 --> 00:01:38,160
It's basically an alternative to alert,

35
00:01:38,160 --> 00:01:41,623
but confirm allows us to answer with yes or no.

36
00:01:42,460 --> 00:01:45,380
And therefore it will return a Boolean value.

37
00:01:45,380 --> 00:01:47,020
We can then enter the message

38
00:01:47,020 --> 00:01:48,677
that should be shown to the user like,

39
00:01:48,677 --> 00:01:51,040
"Do you want to quit?"

40
00:01:51,040 --> 00:01:53,300
And this will open up an alert box,

41
00:01:53,300 --> 00:01:56,410
which already has a yes or no button.

42
00:01:56,410 --> 00:01:58,600
And hence, since this will return a Boolean,

43
00:01:58,600 --> 00:02:00,890
we can store this in "isFinished"

44
00:02:00,890 --> 00:02:04,480
and override this "isFinished" variable value

45
00:02:04,480 --> 00:02:06,663
with the response of the user here.

46
00:02:07,540 --> 00:02:08,646
And if the user chooses,

47
00:02:08,646 --> 00:02:11,087
"Yes," he or she wants to quit,

48
00:02:11,087 --> 00:02:13,240
"isFinished" will be true,

49
00:02:13,240 --> 00:02:16,220
and then "not true" will be false,

50
00:02:16,220 --> 00:02:18,053
and the while loop will quit.

51
00:02:18,890 --> 00:02:23,500
So thereafter, we could "console.log done",

52
00:02:23,500 --> 00:02:28,500
but that will only execute if that while loop stops.

53
00:02:28,540 --> 00:02:31,020
Because as long as this keeps on executing,

54
00:02:31,020 --> 00:02:33,513
this code will not be executed.

55
00:02:34,860 --> 00:02:38,280
So if you save that now and you reload this page,

56
00:02:38,280 --> 00:02:41,200
you get to this prompt, "Do you want to quit?"

57
00:02:41,200 --> 00:02:43,000
And if I click on "Cancel,"

58
00:02:43,000 --> 00:02:45,100
which means no, basically,

59
00:02:45,100 --> 00:02:49,570
I get this prompt again until I finally click, "Okay."

60
00:02:49,570 --> 00:02:52,210
And, at that point, we get done down here

61
00:02:52,210 --> 00:02:56,170
and we don't get this confirmation box anymore.

62
00:02:56,170 --> 00:02:58,560
And again, it's still just a dummy example,

63
00:02:58,560 --> 00:03:03,070
but it shows us really well how a while loop works.

64
00:03:03,070 --> 00:03:07,160
And these are the key loops we have in JavaScript.

65
00:03:07,160 --> 00:03:09,300
Now that we explored them in theory,

66
00:03:09,300 --> 00:03:12,803
let's have a look at more concrete, realistic examples.

