Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Use a for loop to both build an array and iterate through every element in an array.
For Loop Refresher
If you need some help remembering how for
loops work check out the For Loops video.
Code for completed solution
/* 1. Create a function named createRandomList that
uses a for loop to create an array containing 10 random
numbers from 1 to 100 (use the supplied function above
to generate the numbers). The function should return that array. */
function createRandomList() {
let randomList = [];
for (let i=0; i<10; i++) {
randomList.push(random100());
}
return randomList
};
/* 2. Call the createRandomList() function and store
the results in a variable named myRandomList. */
let myRandomList = createRandomList();
console.log(myRandomList);
/* 3. Use a for loop to access each element in the loop. Each
time through the loop log a message to the console that looks something like this:
Item 0 in the array is 48
When you're done you should have output 10 lines to the console -- one for each element.
*/
for (let i=0; i < myRandomList.length; i++) {
console.log('Item ' + i + ' in the array is ' + myRandomList[i]);
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
For this project, you had to create
a function to build an array
0:00
of ten random numbers between one and 100.
0:03
Then you had to call that function
to create an array, then use a for
0:06
loop to access each item in that array.
0:09
Now, we provided the function for
the random number generator.
0:13
If you want to brush up on
the Math.random function,
0:18
check out the teacher's notes for links.
0:20
Let's start by creating
the shell of the function, and
0:23
we'll name that function createRandomList.
0:28
I'll type function createRandomList,
a set of parentheses, and a set of braces.
0:31
Now I'll start by creating an empty
array to hold the random numbers.
0:38
I'll call it randomList.
0:43
In order to add ten random
numbers to the list,
0:51
I'll create a loop that runs ten times and
uses a counter variable named i.
0:53
Now each time through the loop,
the i counter increments by one.
1:07
That's this i+=1 part.
1:11
Now a shorter way to
write that is just i++.
1:14
++ is an incremental operator,
and just adds 1 to the variable.
1:19
Now, if you're bit rusty on for
loops, see the teachers notes, for
1:25
a link to more instruction on them.
1:28
Now within the loop,
I'll call the random 100 function, and
1:31
store the results into a temporary
variable called randomNumber.
1:34
Then to add the number to the randomList
array, just use the array push method,
1:49
like this.
1:54
Type the array name, randomList.push,
1:57
and then I pass randomNumber,
that's the variable.
1:59
Now you could actually
simplify this code a bit,
2:04
you don't need this
randomNumber variable at all.
2:07
It simply holds a value,
2:10
the same value that's returned when
the randomNumber function is called.
2:12
You can skip the variable, and
2:16
simply call the randomNumber
function like this.
2:19
Now outside the loop, we want to return
the ten item random list variable.
2:27
Let's try out this
function by calling it and
2:40
storing the results into a new
variable named myRandomList.
2:43
Type let myRandomList =
createRandomList() with
2:49
a set of parentheses to call the function.
2:54
To see if it worked,
let's log the array to the console.
2:57
I'll type console.log(myRandomList).
3:00
Let's save the file.
3:05
And use node to run it in the console,
3:10
by typing node 2_loop.js.
3:16
And there you have a ten item array.
3:22
How do we know that they're random?
3:25
Well, let me call it again and let's see
if we get different numbers, and we do.
3:26
If your program isn't working, double
check against the code in the teacher's
3:31
notes below and
try to figure out your error.
3:35
Now, the last part of this project is
to use a for loop to iterate through
3:37
the array and print the elements
one line at a time to the console.
3:41
First, I'll add the basic format for
a for loop.
3:46
Now, this loop will run ten times.
3:56
That's fine in this particular case
because the array has ten items, but
3:59
it's not very flexible.
4:03
It'd be better to run this loop
the same number of times as there
4:05
are elements in the array.
4:08
Fortunately, we can get the number of
items in an array using an array's
4:10
length property.
4:13
So just replace 10 here
with myRandomList.length.
4:15
Okay, let's log a string to the console.
4:23
Inside the loop, I'm just gonna
type console.log to begin with.
4:28
Let's just print out a simple string,
Item 0 in the array is 48.
4:34
Let's see what happens,
I'm gonna save this file.
4:38
Clear my console and
run the program again.
4:46
Well, [LAUGH] that isn't very helpful
cuz it prints the exact same string
4:49
over and over again.
4:52
So let's make this a little bit
more tailored to our array.
4:53
So I'm gonna change this 0,
4:59
and using string concatenation,
I'll add our i counter.
5:00
Let's see what happens now.
5:10
A little bit better.
5:15
So we see the index increasing,
0, 1, 2, 3.
5:17
But we're always getting the same number.
5:20
So what we need to do now
is access the element
5:22
at the same index value within the array.
5:25
And we can use the i counter to do that.
5:28
Use string concatenation one more time
5:30
to access myRandomList
at the i index value.
5:35
Let's save this file, Type clear,
5:42
And run this one more time,
and there it is.
5:51
If you had trouble with this
practice session, don't worry.
5:56
Compare your code to the code listed
in the teacher's notes below.
5:59
Look very carefully at
what tripped you up.
6:02
Study it and
figure out how to fix your code.
6:04
Failure is the best way to learn, and
6:07
we don't usually get things
right on the first try.
6:09
As you'll learn later, there are other
array methods that can help you achieve
6:12
similar results, like the for each method.
6:16
As always in programming,
6:19
there are often many different
ways to solve the same problem.
6:20
Ready for the final practice challenge?
6:23
I’ll see you in the next video.
6:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up