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 trialAhmad Hill
Full Stack JavaScript Techdegree Student 1,883 Pointswhy does using an arrow function work but not a function declaration?
I tried to use a function declaration in place of the arrow function and the program didn't run. why doesn't it work?
Ahmad Hill
Full Stack JavaScript Techdegree Student 1,883 Pointscan you use this Alexander Besse?
2 Answers
Peter Vann
36,427 PointsHi Ahmad!
If I understand your question properly, I think the issue comes down to arrow function shorthand syntax as explained here:
To work, this:
const square = num => (num * num) ;
Would need to be converted to this (as a regular function declaration):
const square = function(num) { return (num * num) };
(the return being the key factor in the second code block)
I hope that helps.
Stay safe and happy coding!
Ahmad Hill
Full Stack JavaScript Techdegree Student 1,883 PointsI understand the syntax of an arrow function. my question is pertaining to something else but thanks for the help though.
Courtney Wilson
8,031 PointsYour function getColor()
isn't returning anything:
function getColor() {
Math.floor(Math.random() * 256);
}
Add a return statement to return the random color value for it to work:
function getColor() {
return Math.floor(Math.random() * 256);
}
Alexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 PointsAlexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 PointsHey Ahmad Hill,
Would you be able to share your code that didn't work? This will give us a bit of insight into your code.