1
00:00:01,800 --> 00:00:06,430
So I'm going to go ahead and write the four solutions for these four loops.

2
00:00:06,900 --> 00:00:09,990
So I'm going to make a new file save it.

3
00:00:09,990 --> 00:00:19,080
I'll just call it for loops that HMO and I'll just copy my setup from the while loops.

4
00:00:19,080 --> 00:00:20,460
So very similar.

5
00:00:20,460 --> 00:00:25,470
The only difference is I'm going to change the title to be for loops problem set and then I'm going

6
00:00:25,470 --> 00:00:32,340
to change the script to be for loops dot yes and then I'll make that file.

7
00:00:32,610 --> 00:00:41,190
So make a new file save it for loops dot J us and instead of here as I always like to do

8
00:00:44,010 --> 00:00:48,250
just put something so that we can tell that our loops are connected.

9
00:00:48,270 --> 00:00:50,010
So now if we go ahead and open that up

10
00:00:53,580 --> 00:00:56,560
tells us connected OK.

11
00:00:57,000 --> 00:00:59,230
So the very first problem was DePrince

12
00:00:59,350 --> 00:01:05,560
.

13
00:01:06,360 --> 00:01:12,000
So the very first problem is to print all the numbers between negative 10 and 19.

14
00:01:12,120 --> 00:01:18,360
So I'm going to copy the wild loop code that I have and move it over to four loops and I'm actually

15
00:01:18,360 --> 00:01:24,660
going to make this split screen so that you can see both happening at the same time.

16
00:01:24,660 --> 00:01:30,480
So I have my wild loop version on the left and then my FOR loop version on the right.

17
00:01:30,480 --> 00:01:35,390
So what I need to do is print all the numbers between negative 10 and 19.

18
00:01:36,030 --> 00:01:45,930
So for loop and then just do for I VAR I starts at negative 10 and we're going to keep running while

19
00:01:45,930 --> 00:01:47,620
I is less than 20.

20
00:01:48,110 --> 00:01:56,160
And then we're going to add one every time through the loop con. dog I and that's it.

21
00:01:56,160 --> 00:01:58,340
So it's substantially shorter.

22
00:01:58,350 --> 00:02:00,450
You don't have to make a separate variable.

23
00:02:00,480 --> 00:02:02,820
We don't need a separate line to increment the variable.

24
00:02:02,820 --> 00:02:04,670
We can do it all at once appear.

25
00:02:04,890 --> 00:02:09,430
So let's just make sure that works back to the browser refresh.

26
00:02:09,900 --> 00:02:16,600
Open up the console and we see negative 10 all the way to 19.

27
00:02:16,680 --> 00:02:22,650
So next up printing all the even numbers between 10 and 40.

28
00:02:22,710 --> 00:02:24,680
So there's a few ways we can do this.

29
00:02:24,720 --> 00:02:26,470
We can start we need four.

30
00:02:26,490 --> 00:02:31,790
Either way start i equal to 10 and we want to keep going.

31
00:02:32,070 --> 00:02:36,180
Well I is less than or equal to 40.

32
00:02:36,720 --> 00:02:38,530
So I'm just going to add some spaces.

33
00:02:38,550 --> 00:02:39,930
Technically it doesn't matter.

34
00:02:40,080 --> 00:02:42,200
I just like how it looks.

35
00:02:42,590 --> 00:02:46,130
And then every time through we can either add two.

36
00:02:46,200 --> 00:02:47,580
So I plus two equals two

37
00:02:51,210 --> 00:02:51,990
and that's it.

38
00:02:52,050 --> 00:02:53,470
Contador log I.

39
00:02:53,640 --> 00:02:56,040
And this is the first way of doing it.

40
00:02:56,790 --> 00:03:05,080
And just to demonstrate that back here refresh and we get all the even between 10 and 40.

41
00:03:06,780 --> 00:03:11,970
But again the problem with this solution it's the same problem we had with the first while loop solution

42
00:03:11,970 --> 00:03:12,360
.

43
00:03:12,540 --> 00:03:16,550
If instead I wanted to print the evens between 11 and 40.

44
00:03:16,770 --> 00:03:18,370
I'm going to have a problem.

45
00:03:18,610 --> 00:03:23,190
And that's because I'm just adding to.

46
00:03:26,790 --> 00:03:31,900
And that's because I'm just blindly adding to no matter what the starting value is.

47
00:03:32,250 --> 00:03:34,830
So I'm getting the odd numbers this time.

48
00:03:34,830 --> 00:03:41,280
So if I wanted to refactor it to match the other version where we actually check if a number is even

49
00:03:41,610 --> 00:03:44,560
and needs to look like this I add one every time.

50
00:03:44,910 --> 00:03:49,240
And then I have an if statement and I say if the number is even.

51
00:03:49,320 --> 00:03:56,280
So if I had two equals zero then we'll come to that log.

52
00:03:56,280 --> 00:03:58,830
I just like that.

53
00:04:00,060 --> 00:04:05,940
And we shouldn't notice a difference very fresh still works just the same that this is the more foolproof

54
00:04:05,940 --> 00:04:10,280
way where I can change this to be an odd number and it still doesn't matter.

55
00:04:10,290 --> 00:04:15,820
Now it just prints the evens starting from 13 to 40.

56
00:04:15,830 --> 00:04:17,410
All right.

57
00:04:17,430 --> 00:04:24,240
The next one printing all the odd numbers between 300 and 333.

58
00:04:24,330 --> 00:04:26,380
So we want another for loop.

59
00:04:26,670 --> 00:04:31,070
We'll start i equal to 300.

60
00:04:31,170 --> 00:04:32,200
We want to keep going.

61
00:04:32,250 --> 00:04:41,870
Well I asked less than or equal to 3 3 3 and we'll just add one every time.

62
00:04:43,650 --> 00:04:48,810
And then what we'll do just like we did up here instead of checking if something is even Well check

63
00:04:48,810 --> 00:04:49,690
if it's odd.

64
00:04:49,860 --> 00:04:51,040
So it's very similar.

65
00:04:51,240 --> 00:04:59,100
If I my two is not equal to zero that means that it's odd it's not divisible by two then we'll cancel

66
00:04:59,100 --> 00:05:11,430
that log and say that refresh and we get all the odd numbers starting at 3 or 1 up to 3 133.

67
00:05:12,330 --> 00:05:19,770
So the very last one printing all the numbers divisible by five and three that are between five and

68
00:05:19,770 --> 00:05:20,740
50.

69
00:05:21,210 --> 00:05:25,950
So another for loop we start or I equal to five.

70
00:05:25,980 --> 00:05:33,230
We keep going while it's less than or equal to 50 and we add one at the end of every loop.

71
00:05:33,510 --> 00:05:38,490
Then what we're going to do is have an if statement check if I is divisible by five

72
00:05:41,460 --> 00:05:50,460
then we're going to check if it also divisible by three I like that.

73
00:05:50,460 --> 00:05:55,530
And then we cancel the log and that's it we're done.

74
00:05:55,530 --> 00:05:57,170
So let's check it out.

75
00:05:57,570 --> 00:06:01,990
Refresh and you can see we get 15 30 and 45.

76
00:06:02,400 --> 00:06:08,730
So to recap here on the left I have the while loop solutions and on the right I have the for loop solutions

77
00:06:09,030 --> 00:06:13,350
so I'll try and get them to match up as perfectly as possible

78
00:06:13,350 --> 00:06:20,760
.

79
00:06:20,760 --> 00:06:21,510
There we go.

80
00:06:21,900 --> 00:06:26,090
And you can see the for loop solutions are considerably shorter.
