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 trialHarry James
14,780 PointsCan't set x to 1 in Global Scope?
Hello,
I have the question
When the 'hypotenuse' function is called the value of 'x' changes from 1. Fix it so that 'x' is still 1 in the global scope.
However, I, for some reason can not set x to 1 in the Global Scope.
Here is the program I have written:
var x = 1;
var y = 1;
function hypotenuse(a , b) {
var cSquared = a * a + b * b;
x = 1;
return x;
}
hypotenuse(x, y);
However, I get the error message: Bummer! Exepcting the return value of hypotenuse(1,1) to be 1.4142135623730951 but got 1 So, if I set x to the value of 1.4142135623730951, I then get the error message The global variable x was 1.4142135623730951 not 1
Any help would be appreciated.
2 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsYou're not supposed to change the value of x
in the global scope. It's already 1
. You have to make the x
variable inside the function a local variable.
To achieve that, the only change you have to do in the challenge is add the keyword var
in front of x
on line 22.
ilyass mougar
Courses Plus Student 2,858 PointsTry this is true 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);
Harry James
14,780 PointsHarry James
14,780 PointsAh, I got confused by the "so that 'x' is still 1 in the global scope" in the question. That makes sense now, thanks for explaining.