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 JavaScript Foundations Functions Scope

Harry James
Harry James
14,780 Points

Can'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

You'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.

Harry James
Harry James
14,780 Points

Ah, 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.

Try 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);