Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Basic Arrays in JavaScript!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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 up-
Nicholas Wallen
12,278 Points1 Answer
-
Leo Marco Corpuz
18,975 Points1 Answer
-
Mike Hatch
14,940 Points1 Answer
-
Meg Dooley
7,321 PointsWhat's wrong with my code?! Here's a link to my code for challenge 2. https://w.trhou.se/vvo4fyntfc
Posted by Meg DooleyMeg Dooley
7,321 Points2 Answers
-
Daniele Fusari
40,669 Points1 Answer
-
Philipp Kushnir
1,751 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
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