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 trialdaniwao
13,125 PointsGlobal scope variable question
I'm not sure why I'm getting an error in this code challenge. It's asking me to make a global variable where x = 1;
Here's my code below.
x = 1;
var y = 1;
function hypotenuse(a , b) {
var cSquared = a * a + b * b;
x = ;
x = Math.sqrt(cSquared);
return x;
}
hypotenuse(x, y);
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Dan,
This was the starter code for this:
var x = 1;
var y = 1;
function hypotenuse(a , b) {
var cSquared = a * a + b * b;
x = Math.sqrt(cSquared);
return x;
}
hypotenuse(x, y);
The instructions are asking you to make sure that changing x
inside the function doesn't change the global x
at the top. In other words, you need to declare x
as a local variable inside that function so that changes to x
inside the function don't affect the x
that is global at the top.
var x = 1;
var y = 1;
function hypotenuse(a , b) {
var x;
var cSquared = a * a + b * b;
x = Math.sqrt(cSquared);
return x;
}
hypotenuse(x, y);
jase richards
10,379 PointsBy the looks of it
x isn't declared as a variable
You have var y = 1;
But no var for the x
Chris Shaw
26,676 PointsHi dan,
You appear to be missing the var
keyword from your first line which currently just says x = 1;
which will fail as the test is looking for var
first.
Also x = ;
will fail as you can't assign nothing to a variable.