1 00:00:00,250 --> 00:00:03,150 For this project, you had to create a function to build an array 2 00:00:03,150 --> 00:00:06,220 of ten random numbers between one and 100. 3 00:00:06,220 --> 00:00:09,990 Then you had to call that function to create an array, then use a for 4 00:00:09,990 --> 00:00:13,030 loop to access each item in that array. 5 00:00:13,030 --> 00:00:15,540 Now, we provided the function for the random number generator. 6 00:00:18,430 --> 00:00:20,912 If you want to brush up on the Math.random function, 7 00:00:20,912 --> 00:00:23,560 check out the teacher's notes for links. 8 00:00:23,560 --> 00:00:28,540 Let's start by creating the shell of the function, and 9 00:00:28,540 --> 00:00:31,680 we'll name that function createRandomList. 10 00:00:31,680 --> 00:00:37,450 I'll type function createRandomList, a set of parentheses, and a set of braces. 11 00:00:38,540 --> 00:00:43,300 Now I'll start by creating an empty array to hold the random numbers. 12 00:00:43,300 --> 00:00:46,866 I'll call it randomList. 13 00:00:51,170 --> 00:00:53,967 In order to add ten random numbers to the list, 14 00:00:53,967 --> 00:00:58,510 I'll create a loop that runs ten times and uses a counter variable named i. 15 00:01:07,440 --> 00:01:11,745 Now each time through the loop, the i counter increments by one. 16 00:01:11,745 --> 00:01:14,587 That's this i+=1 part. 17 00:01:14,587 --> 00:01:18,350 Now a shorter way to write that is just i++. 18 00:01:19,810 --> 00:01:25,360 ++ is an incremental operator, and just adds 1 to the variable. 19 00:01:25,360 --> 00:01:28,608 Now, if you're bit rusty on for loops, see the teachers notes, for 20 00:01:28,608 --> 00:01:31,210 a link to more instruction on them. 21 00:01:31,210 --> 00:01:34,230 Now within the loop, I'll call the random 100 function, and 22 00:01:34,230 --> 00:01:38,740 store the results into a temporary variable called randomNumber. 23 00:01:49,530 --> 00:01:54,465 Then to add the number to the randomList array, just use the array push method, 24 00:01:54,465 --> 00:01:55,210 like this. 25 00:01:57,110 --> 00:01:59,780 Type the array name, randomList.push, 26 00:01:59,780 --> 00:02:03,510 and then I pass randomNumber, that's the variable. 27 00:02:04,640 --> 00:02:07,210 Now you could actually simplify this code a bit, 28 00:02:07,210 --> 00:02:09,650 you don't need this randomNumber variable at all. 29 00:02:10,940 --> 00:02:12,430 It simply holds a value, 30 00:02:12,430 --> 00:02:16,320 the same value that's returned when the randomNumber function is called. 31 00:02:16,320 --> 00:02:19,718 You can skip the variable, and 32 00:02:19,718 --> 00:02:25,350 simply call the randomNumber function like this. 33 00:02:27,140 --> 00:02:32,770 Now outside the loop, we want to return the ten item random list variable. 34 00:02:40,675 --> 00:02:43,300 Let's try out this function by calling it and 35 00:02:43,300 --> 00:02:46,900 storing the results into a new variable named myRandomList. 36 00:02:49,710 --> 00:02:54,730 Type let myRandomList = createRandomList() with 37 00:02:54,730 --> 00:02:57,290 a set of parentheses to call the function. 38 00:02:57,290 --> 00:03:00,028 To see if it worked, let's log the array to the console. 39 00:03:00,028 --> 00:03:05,700 I'll type console.log(myRandomList). 40 00:03:05,700 --> 00:03:06,680 Let's save the file. 41 00:03:10,650 --> 00:03:16,237 And use node to run it in the console, 42 00:03:16,237 --> 00:03:20,929 by typing node 2_loop.js. 43 00:03:22,520 --> 00:03:25,690 And there you have a ten item array. 44 00:03:25,690 --> 00:03:26,910 How do we know that they're random? 45 00:03:26,910 --> 00:03:31,870 Well, let me call it again and let's see if we get different numbers, and we do. 46 00:03:31,870 --> 00:03:35,040 If your program isn't working, double check against the code in the teacher's 47 00:03:35,040 --> 00:03:37,860 notes below and try to figure out your error. 48 00:03:37,860 --> 00:03:41,840 Now, the last part of this project is to use a for loop to iterate through 49 00:03:41,840 --> 00:03:45,170 the array and print the elements one line at a time to the console. 50 00:03:46,950 --> 00:03:50,530 First, I'll add the basic format for a for loop. 51 00:03:56,170 --> 00:03:58,610 Now, this loop will run ten times. 52 00:03:59,990 --> 00:04:03,790 That's fine in this particular case because the array has ten items, but 53 00:04:03,790 --> 00:04:05,340 it's not very flexible. 54 00:04:05,340 --> 00:04:08,260 It'd be better to run this loop the same number of times as there 55 00:04:08,260 --> 00:04:10,220 are elements in the array. 56 00:04:10,220 --> 00:04:13,840 Fortunately, we can get the number of items in an array using an array's 57 00:04:13,840 --> 00:04:15,120 length property. 58 00:04:15,120 --> 00:04:23,120 So just replace 10 here with myRandomList.length. 59 00:04:23,120 --> 00:04:25,800 Okay, let's log a string to the console. 60 00:04:28,410 --> 00:04:34,190 Inside the loop, I'm just gonna type console.log to begin with. 61 00:04:34,190 --> 00:04:38,050 Let's just print out a simple string, Item 0 in the array is 48. 62 00:04:38,050 --> 00:04:42,352 Let's see what happens, I'm gonna save this file. 63 00:04:46,034 --> 00:04:49,180 Clear my console and run the program again. 64 00:04:49,180 --> 00:04:52,480 Well, [LAUGH] that isn't very helpful cuz it prints the exact same string 65 00:04:52,480 --> 00:04:53,890 over and over again. 66 00:04:53,890 --> 00:04:59,058 So let's make this a little bit more tailored to our array. 67 00:04:59,058 --> 00:05:00,290 So I'm gonna change this 0, 68 00:05:00,290 --> 00:05:04,300 and using string concatenation, I'll add our i counter. 69 00:05:10,660 --> 00:05:12,160 Let's see what happens now. 70 00:05:15,670 --> 00:05:17,260 A little bit better. 71 00:05:17,260 --> 00:05:20,400 So we see the index increasing, 0, 1, 2, 3. 72 00:05:20,400 --> 00:05:22,080 But we're always getting the same number. 73 00:05:22,080 --> 00:05:25,340 So what we need to do now is access the element 74 00:05:25,340 --> 00:05:28,380 at the same index value within the array. 75 00:05:28,380 --> 00:05:30,718 And we can use the i counter to do that. 76 00:05:30,718 --> 00:05:35,860 Use string concatenation one more time 77 00:05:35,860 --> 00:05:41,790 to access myRandomList at the i index value. 78 00:05:42,900 --> 00:05:48,558 Let's save this file, Type clear, 79 00:05:51,080 --> 00:05:56,510 And run this one more time, and there it is. 80 00:05:56,510 --> 00:05:59,380 If you had trouble with this practice session, don't worry. 81 00:05:59,380 --> 00:06:02,670 Compare your code to the code listed in the teacher's notes below. 82 00:06:02,670 --> 00:06:04,850 Look very carefully at what tripped you up. 83 00:06:04,850 --> 00:06:07,780 Study it and figure out how to fix your code. 84 00:06:07,780 --> 00:06:09,580 Failure is the best way to learn, and 85 00:06:09,580 --> 00:06:12,610 we don't usually get things right on the first try. 86 00:06:12,610 --> 00:06:16,200 As you'll learn later, there are other array methods that can help you achieve 87 00:06:16,200 --> 00:06:19,050 similar results, like the for each method. 88 00:06:19,050 --> 00:06:20,350 As always in programming, 89 00:06:20,350 --> 00:06:23,670 there are often many different ways to solve the same problem. 90 00:06:23,670 --> 00:06:25,620 Ready for the final practice challenge? 91 00:06:25,620 --> 00:06:26,770 I’ll see you in the next video.