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 trialDavid Regel
Full Stack JavaScript Techdegree Student 5,504 PointsWhat is wrong with my function?
I'm trying to finish this task:
Create a function that calculates the area of a rectangle. The function should accept the width and height as arguments and return the area of that rectangle. The area of a rectangle is the width * height.
My attempt looks like this:
function rectangle() {
var width = parseInt(prompt("What is the width of this rectangle?"));
var height = parseInt(prompt("What is the height of this rectangle?"));
var area = width*height;
return = area;
}
rectangle();
According to my Console, something is wrong with the code "return = area;" at the end of my function. It says: Uncaught SyntaxError: Unexpected token =
Can someone help me out?
2 Answers
Thomas Gauperaa
3,584 PointsTry "return area;"
Thomas Gauperaa
3,584 PointsYes, var area is defined within the function so it isn't available.
Thomas Gauperaa
3,584 PointsYou could try:
const width = parseInt(prompt("What is the width of this rectangle?"));
const height = parseInt(prompt("What is the height of this rectangle?"));
function rectangle(width, height) {
return width*height;
}
document.write(rectangle(width, height));
David Regel
Full Stack JavaScript Techdegree Student 5,504 PointsPerfect, thank you so much!
David Regel
Full Stack JavaScript Techdegree Student 5,504 PointsDavid Regel
Full Stack JavaScript Techdegree Student 5,504 PointsWorked, this particular problem is solved - thank you! Unfortunately, something else is not working now. I can't see the value of my variable "area". I added one more line in my function: document.write(area); Do you know what I just did wrong here? :)