Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

My solution.....different then the video but it works. Not sure if something is wrong with it or not

```/* INSTRUCTIONS To run this file, click in the Console below and type: node 2_loop.js If the console isn't visible, click the View menu above and choose Show Console.

You can clear the console by typing clear and pressing enter.

If your program is stuck in an infinite loop, you can break out of the program by typing ctrl + C. */

/*Note: We've supplied you a basic function for generating a random number from 1 to 100 */ function random100() { return Math.floor(Math.random() * 100) + 1; }

/* 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. / / 2. Call the createRandomList() function and store the results in a variable named 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. */

let arr = []; function createRandomList() { for (let i = 0; i < 10; i++) { arr.push(random100()); } };

createRandomList(arr);

let itemNum = 0; for (let y = 0; y < 10; y++) { itemNum++ console.log(Item ${itemNum} in the array is ${arr[y]}) }

// Run your code by typing node 2_loop.js in the console below

This is my solution and it runs fine with the desired result.

1 Answer

Steven Parker
Steven Parker
231,269 Points

Your code produces the correct logged output, but doesn't quite meet the requirements of the instructions:

  1. Create a function named createRandomList that ... should return that array.
  2. Call the createRandomList() function and store the results in a variable named myRandomList.

So the "arr" variable could be completely internal to the createRandomList function, and then passed back as a return value.

Then myRandomList would be used by the final loop to log the values to the console.