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

Thomas Moore
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Moore
Front End Web Development Techdegree Graduate 25,371 Points

Arrow function not working in this practice lesson

I'm following along with this course, and trying to complete it using arrow function syntax instead of the old function syntax used in the video. I'm running two versions of the code, and they're exactly the same apart from the arrow syntax.

When I run the arrow syntax, I get an error of 'undefined'. I think this is something to do with the scope of the variables declared within the function, but I can't be sure. Any help would be greatly appreciated.

Thanks!

Bonus points for anyone who can advise why in Task #2 createRandomList needs to be converted into another variable (myRandomList) before being logged in the console.

Function syntax from course (works)

This logs to the console the array: [ 49, 80, 37, 68, 28, 10, 4, 89, 51, 78 ]

/* 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 <= 9; i += 1) {
    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);

Arrow syntax (causes error, see below)

This is functionally identical, but in the arrow syntax causes an error "undefined".

/* 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. */
const createRandomList = () => {
  var randomList = [];
  for (let i = 0; i <= 9; i += 1) { 
  randomList.unshift(random100);
  }
  var randomList;
}

/* 2. Call the createRandomList() function and store the results in a variable named myRandomList. */
let myRandomList = createRandomList();
console.log(myRandomList);
Charles Badger
Charles Badger
18,189 Points

In your second example, your function has no return value. so when you try to store it's output into myRandomList, it doesn't know what to put in there, so it just defines it as 'undefined'

2 Answers

Mikołaj Gonciarz
Mikołaj Gonciarz
8,078 Points

I assume random100 is some kind of func returning random number? In this case it is not the case with AF but with some mistypingL: You forgot parenthesis in callin random100() After loop you are declaring instead of returning the value

Thomas Moore
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Moore
Front End Web Development Techdegree Graduate 25,371 Points

Thanks both very much for the answers. You've genuinely helped me figure out what's going on with the syntax and why the script is written that way.